我使用 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 anSTPCardParams
object with the information you've collected. Then, useSTPAPIClient
to convert that into anSTPToken
which you can then pass to your server. (Note: the examples in this guide assume you've written a function calledsubmitTokenToBackend:completion:
which uses a networking library to POST yourSTPToken
to your server).
关于ios - 更改 STPPaymentCardTextField 的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37730702/