ios - 更改 STPPaymentCardTextField 的布局

标签 ios swift uitextfield stripe-payments

我使用 Stripe 文档中显示的步骤将 TextField 添加到我的 View 中。 但是当我加载模拟器或在我的手机上运行应用程序时,文本字段越界并且 CVC 文本字段不显示在移动设备上。 有没有办法更改文本字段的布局,让子项(卡号、有效期和 CVC 号)全部按列而不是行设置?

import UIKit
import Stripe

class ViewController: UIViewController, STPPaymentCardTextFieldDelegate {
    let paymentTextField = STPPaymentCardTextField()

    override func viewDidLoad() {
        super.viewDidLoad()
        paymentTextField.frame = CGRectMake(15, 15, CGRectGetWidth(self.view.frame) - 30, 44)
        paymentTextField.delegate = self


        view.addSubview(paymentTextField)
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func paymentCardTextFieldDidChange(textField: STPPaymentCardTextField) {
        // Toggle navigation, for example
//        saveButton.enabled = textField.valid
    }

}

最佳答案

您遇到的问题可能与使用自动布局有关。如果您在调用 viewDidLoad 方法时使用自动布局(在新项目中被缺陷使用),则尚未计算布局中的 View 大小,以便使用计算出的正确大小应用自动布局约束后,您将不得不使用 viewDidLayoutSubviews 回调(但您不能在那里添加 Stripe 字段)。

如果您想继续使用 STPPaymentCardTextField,我建议的解决方案是使用固定大小的容器 View ,然后以固定宽度向其中添加 STPPaymentCardTextField

另一方面,回答你的问题,没有办法将 STPPaymentCardTextField 更改为按列而不是按行,但你可以自己实现它并使用 Stripe API 客户端请求 token (来自:https://stripe.com/docs/mobile/ios/custom ):

STPAPIClient + STPCardParams: If you're collecting your users' card details by using STPPaymentCardTextField or building your own credit card form, you can use the STPAPIClient class to handle tokenizing those details. First, assemble an STPCardParams object with the information you've collected. Then, use STPAPIClient to convert that into an STPToken which you can then pass to your server. (Note: the examples in this guide assume you've written a function called submitTokenToBackend:completion: which uses a networking library to POST your STPToken to your server).

关于ios - 更改 STPPaymentCardTextField 的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37730702/

相关文章:

objective-c - MPMoviePlayerController 生成本地视频文件的缩略图并存储

ios - 添加 UITextField 内联 UILabel(填空)

iphone - 从自定义单元格 iPhone 中的 textField 获取值

iphone - 将 View Controller 类拆分为 VC 和自定义类的规则是什么?

ios - 如何在 didSelectRow 之后关闭 UISearchController?

ios - Xcode UITableView 放置和单元格分隔符

swift - 等到添加通知后再继续我的其余代码?

ios - UINavigationController Push Segue 在 iOS11 中不起作用,没有后退按钮,没有标题

ios - wifi不存在时NEHotspotConfigurationManager报错问题

ios - 检测文本字段应该从TableViewController中的Custom UITableViewCell返回以添加新行