【问题标题】:How to create PKPaymentButton in IB?如何在 IB 中创建 PKPaymentButton?
【发布时间】:2016-06-03 12:09:10
【问题描述】:

以下代码来自 WWDC 2015 Session 702 Apple Pay Within Apps - Emporium: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)
    }

我想知道我们是否可以在 IB 中创建这个按钮。

【问题讨论】:

  • 只需将您的按钮添加到 PKPaymentButton 的子类
  • 以前的版本支持apple pay,但是不支持这个按钮怎么办?这意味着您将如何在 xib 中初始化一个有条件地子类为 PKPaymentButton 的按钮?

标签: ios swift interface-builder applepay


【解决方案1】:

我也在寻找这个,结果证明可以使用 IB 来设置它。 只需在 IB 中创建一个 UIButton,将其类型设置为 PKPaymentButton,现在在 UserDefinedRuntimeAttributes 中根据需要设置“样式”和“类型”,对应于 PKPaymentButtonStyle 和 PKPaymentButtonType。 例如:

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

Example Setup

这就是你最终得到的:

Example Button

PS。有趣的是,您可以在 IB 属性中设置cornerRadius,但正如您所见,它不会影响按钮。似乎即使在代码中设置它也无济于事。因此,如果您不想被矩形 PKPaymentButton 卡住 - 只需将其放在一个视图中,将其圆角并确保在 IB 中打勾 Clip to Bounds(或在代码 yourButtonHolder.clipsToBounds = true 中)

【讨论】:

  • 不要忘记将 PassKit.framework 添加到项目的构建阶段 --> 将二进制文件与库链接。
【解决方案2】:

您可以在 Interface Builder 的 Identity Inspector 中更改按钮的类,方法是编辑 Custom Class 字段。此时您为其创建的 outlet 应该是 PKPaymentButton 类型。

【讨论】:

  • IB如何设置PKPaymentButtonStyle?
  • 你找到方法了吗@chrish
  • 这不是最好的方法。您将无法更改按钮类型或样式。而且默认按钮的外观毫无用处。
【解决方案3】:

是的,我们可以使用下面的代码行来设置样式和按钮类型。

applePayButton.setValue(paymentButtonStyle.rawValue, forKey: "style") applePayButton.setValue(2, forKey: "type")

【讨论】:

    猜你喜欢
    • 2020-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多