1

When I add the PKAddPassButton to any project, the text and the icon look unnaturally large. The designers on my team feel sick looking at it and I don't blame them. To prove a point I created a blank project and this is how the button looks:

enter image description here

If you compare it to Apple examples (page 3 here https://developer.apple.com/wallet/Add-to-Apple-Wallet-Guidelines.pdf) the text is significantly small.

The code is very minimal. I have a button on a storyboard from which I take a frame for my PKAddPassButton.

import UIKit
import PassKit

class ViewController: UIViewController {

    @IBOutlet weak var button: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        let pkButton = PKAddPassButton()
        view.addSubview(pkButton)
        pkButton.frame = button.frame
        button.isHidden = true
    }
}
Michał Kreft
  • 548
  • 3
  • 16
  • Here the pkButton has button.frame. Have you tried changing this value. – Sachin Vas Feb 22 '17 at 15:37
  • I'm not sure what you mean @Sahin. I did try to display it with different size but it always looks like this. – Michał Kreft Feb 22 '17 at 17:00
  • 1
    @MichałKreft I need lil help, I have used same code to create the button but icon is not appearing any idea ? – Shailesh Chandavar Apr 12 '18 at 05:57
  • @MichałKreft I know this has been a while ago. But where did you get the button above that is a One-line button instead of a Two-line button? The One-line button is no longer found in the apple documentation to download the Badges and Guidlines from here: https://developer.apple.com/wallet/ there is only a Two-line button. – jeancode Apr 08 '20 at 21:46
  • @jeancode I don't recall that now well. But when I go to developer.apple.com/wallet and click on guidelines, it points me to https://developer.apple.com/wallet/Add-to-Apple-Wallet-Guidelines.pdf where you can still see both version on page 3. Maybe it scales automatically based on how much width you give? – Michał Kreft Apr 09 '20 at 10:47

1 Answers1

4

It's odd that the sizing is so wrong to start with. I verified that changing the frame of the button does not resize the label or the icon.

Probably the best option would be to apply a scale transform to the button. This would work:

let scale = CGFloat(floatLiteral: 0.75)
pkButton.transform = CGAffineTransform(scaleX: scale, y: scale)

I don't necessarily recommend this, but I did verify that you can adjust the font size manually:

let label = pkButton.value(forKey: "singleLineLabel")! as! UILabel
let label2 = pkButton.value(forKey: "multiLineLabel")! as! UILabel
label.font = UIFont(name: label.font.fontName, size: 8)
label2.font = UIFont(name: label2.font.fontName, size: 8)

I haven't tried the icon, but I suspect you can do something similar.

aturan23
  • 4,798
  • 4
  • 28
  • 52
David S.
  • 6,567
  • 1
  • 25
  • 45
  • A bit hackish, but I'm gonna give it a try. Might be the only option. It looks me like everything on the button is upscaled twice. Font size, image and also the corner radius looks twice as big as it should. I wonder if there is a single property that could influence it. – Michał Kreft Feb 23 '17 at 09:29
  • That's an interesting thought. I am amending my answer with another choice -- a CGAffineTransform on the button. – David S. Feb 23 '17 at 18:48
  • This hack actually works very well. If you could change your answer to use scale CGFloat(floatLiteral: 0.75) as this scale makes it look like it should by Apple Design specs. Also the transform line doesn't work for me as there is no such method in my SDK. I use CGAffineTransformMakeScale(scale, scale) – Michał Kreft Feb 24 '17 at 10:39
  • I think CGAffineTransformMakeScale is swift 2, and in Swift 3 it is how I have it above. CGAffineTransformMakeScale results in a compilation error in Xcode 8.2.1 and recommends changing it to what I have above. – David S. Feb 24 '17 at 15:52
  • OK, makes sense as I'm still on Swift 2. Cheers David! – Michał Kreft Feb 25 '17 at 10:01
  • 1
    Is this still the best solution to this? It's so odd that the button subviews don't scale themselves... – venturidoo Oct 10 '18 at 23:38