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.
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
)
Yes we can set and style and button type can be achieved using below lines of code.
applePayButton.setValue(paymentButtonStyle.rawValue, forKey: "style") applePayButton.setValue(2, forKey: "type")
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With