Repurpose existing UIButton as Apple Pay Button in Swift

490 Views Asked by At

In our checkout process, we have an existing UIButton for submitting an order. As we are starting to support Apple Pay, we want to recycle the existing button and show the Apple Pay option.

Within storyboard, we have placed a UIButton and connected it to our UIViewController class. Instead of initializing another button specifically for PKPaymentButton, I want to do something like:

submit_order_btn = PKPaymentButton(paymentButtonType: .buy, paymentButtonStyle: .black)

However, this is not working and throwing a warning: Instance will be immediately deallocated because property 'submit_order_btn' is 'weak'.

As we have some basic validation checking happening when the button is pressed, I would like to simply update the button styling itself without affecting any associated actions with the button.

I have also created the PKPaymentButton and added that to the existing button as a subview but it breaks the button functionality for other workflows and I have to track the subviews now to pop/push into the UIButton.

apple_pay_button = PKPaymentButton(paymentButtonType: .inStore, paymentButtonStyle: .black)
            apple_pay_button.frame = CGRect.init(x: 10, y: 10, width: submit_order_btn.frame.width-20, height: submit_order_btn.frame.height-20)
            submit_order_btn.addSubview(apple_pay_button)

I am hoping there is a better way of doing this where I can simply update the UIButton styling to match the PKPaymentButton. I don't want to recreate the button manually as well because it is against Apple's recommendations.

0

There are 0 best solutions below