Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create PKPaymentButton in IB?

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.

like image 455
koira Avatar asked Jun 03 '16 12:06

koira


3 Answers

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 )

like image 105
cloudjubei Avatar answered Sep 17 '22 04:09

cloudjubei


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")

like image 39
Ravi Kishore Avatar answered Sep 18 '22 04:09

Ravi Kishore


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.

like image 34
Wyatt Avatar answered Sep 20 '22 04:09

Wyatt