条纹布局ios键盘默认隐藏。

时间:2014-03-14 12:56:17

标签: ios stripe-payments

我正在使用条纹SDK,它会为卡片详细信息创建视图。代码如下:

//live key
NSString *cardApiKey;

if([hostType isEqualToString:@"production"])
{
    cardApiKey = @"xxxxxxxxxxxxxxxxxxxxx";
} else {
    cardApiKey = @"xxxxxxxxxxxxxxxxxxxxx";
}

self.stripeView = [[STPView alloc] initWithFrame:CGRectMake(10, _cardDetailsLabel.frame.origin.y+_cardDetailsLabel.frame.size.height+10, 200, 55) andKey:cardApiKey];

[self.canvasView addSubview:self.stripeView];

self.stripeView.delegate = self;

问题是数字键盘键盘默认显示。我不希望每次初始化stripeview时都会弹出数字键盘。我怎么能绕过这个?谢谢。

2 个答案:

答案 0 :(得分:5)

方法1:使用自定义视图

您可以使用自定义视图而不是STPView,它可以让您完全控制键盘何时被触发。然后,您可以只填充STPCart的实例。条纹gighub页面在以下部分描述了这种方法:'使用您自己的视图'

https://github.com/stripe/stripe-ios

STPCard *card = [[STPCard alloc] init];
card.number = @"4242424242424242";
card.expMonth = 12;
card.expYear = 2020;

方法2:使用Stripes STPView实现

要使用Stripe的STPView,您可以在违规的UITextField子类上使用addTarget方法来处理UIControlEventEditingDidBegin事件。您可以在视图加载后将其删除,因为它不再需要:

// YourViewController.h

#import <UIKit/UIKit.h>
#import "YourViewController.h"

@class STPView;

@interface YourViewController : UIViewController

@property (strong,nonatomic) STPView *stripeView;

@end

// YourViewController.m

#import "YourViewController.h"
#import "STPView.h"
#import "PKView.h"
#import "PKTextField.h"

@interface YourViewController ()
@end

@implementation YourViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Create your STPView instance
    _stripeView = [[STPView alloc] initWithFrame:CGRectMake(15,20,290,55) andKey:@"perishable_key"];
    [self.view addSubview:_stripeView];
    // Add target for editingDidBegin UIControl event
    [_stripeView.paymentView.cardNumberField addTarget:self action:@selector(editingBegan:) forControlEvents:UIControlEventEditingDidBegin];
}

- (void)editingBegan:(id)sender
{
    // Hide the keyboard
    [_stripeView.paymentView.cardNumberField resignFirstResponder];
    // Remove the addTarget
    [_stripeView.paymentView.cardNumberField removeTarget:self action:@selector(editingChanged:) forControlEvents:UIControlEventEditingDidBegin];
}

@end

cardNumberField 是PKTextField(UITextField的子类)的实例,以及在创建STPView时获取焦点的视图。

答案 1 :(得分:4)

所以,正如评论中所提到的,行为是焦点在初始化时自动转到STPView的creditCard textField。因此,当焦点位于textField中时,键盘会被触发。因此,解决方法应该是我们需要使textField在添加到视图时失去焦点。试试这样:

-(void)viewWillLayoutSubviews
{
    [self.stripeView endEditing:YES]; //This sets whatever textFields inside (though we have only one) lose focus when the self.stripeView added to its parentView.
}