10

The following code is from WWDC 2015 Session 702 Apple Pay Within Apps - Emporium: A Simple Shopping Experience with Apple Pay

    if PKPaymentAuthorizationViewController.canMakePaymentsUsingNetworks(ProductTableViewController.supportedNetworks) {

        let button = PKPaymentButton(type: .Buy, style: .Black)
        button.addTarget(self, action: #selector(ProductTableViewController.applePayButtonPressed), forControlEvents: .TouchUpInside)

        button.center = applePayView.center
        button.autoresizingMask = [.FlexibleLeftMargin, .FlexibleRightMargin]
        applePayView.addSubview(button)
    }

I wonder if we could create this button in IB.

Eric
  • 1,207
  • 7
  • 22
koira
  • 977
  • 7
  • 10
  • 1
    just add your button to sub class of PKPaymentButton – Anbu.Karthik Jun 03 '16 at 12:28
  • What about the previous versions that support apple pay, but not this button ? Meaning how would you initialize a button in xib that conditionally subclasses to PKPaymentButton ? – Mehul Parmar Nov 29 '16 at 04:57

2 Answers2

5

I was looking for this as well and it turns out it is possible to use IB to set this. Just create a UIButton in IB, set its type to PKPaymentButton and now in the UserDefinedRuntimeAttributes set "style" and "type" as wanted, corresponding to PKPaymentButtonStyle and PKPaymentButtonType. For example:

type = 1 //corresponding to - PKPaymentButtonTypeBuy
style = 2 //corresponding to - PKPaymentButtonStyleBlack

Example Setup

And this is what you end up getting:

Example Button

PS. Interestingly, you can set cornerRadius in the IB Attributes, but as you can see it doesn't affect the button. It seems that even setting it in code doesn't do anything. So if you don't want to be stuck with a rectangular PKPaymentButton - just put it in a view, whose corners you round and make sure you tick Clip to Bounds in IB (or in code yourButtonHolder.clipsToBounds = true )

cloudjubei
  • 53
  • 2
  • 7
  • Don't forget to add PassKit.framework to your project's Build Phases --> Link Binary With Libraries. – Quy Bui Nov 18 '20 at 20:42
2

You can change the class of the button in the Identity Inspector in Interface Builder, by editing the Custom Class field. The outlet you create for it at that point should be of type PKPaymentButton.

Wyatt
  • 309
  • 2
  • 11