自定义UIAlertView在首次出现后不会调整键盘的大小

时间:2014-08-13 19:09:53

标签: ios uialertview

我有一个自定义的UIAlertVIew,我用2个textField来输入值。第一次显示customUIAlertVIew时,视图会向上移动以为键盘腾出空间。 enter image description here

遗憾的是,不是每个后续显示的情况。随后的每一次,alertView都不会向上移动键盘。 enter image description here

我正在寻找导致此问题的原因,以及如何在将来解决这个问题。

编辑:以下是如何创建自定义UIAlertVIew的代码。 CustomUIAlertView.m

-(id)initWithTitle:(NSString *)title {
    if (self = [super init]){
        self.title = title;
        self.message = @"\n\n\n\n\n\n";
        [self addButtonWithTitle:@"Cancel"];
        [self addButtonWithTitle:@"Done"];
        [self setDelegate:self];

    }
    return self;
}

-(void)didPresentAlertView:(UIAlertView *)alertView{
    NSArray *subViews = [UIApplication sharedApplication].keyWindow.rootViewController.
    presentedViewController.view.subviews;

    if (subViews.count >1){
        UIView *presentedView = [subViews objectAtIndex:1];

        UILabel *player1Label = [[UILabel alloc]initWithFrame:CGRectMake(20, 55, 130, 21)];
        player1Label.text = [NSString stringWithFormat:@"Player 1 Score"];
       // player1Label.textInputMode = UIKeyboardTypeNumberPad;

        player1Label.tag = 42;
        [presentedView addSubview:player1Label];

        UILabel *player2Label = [[UILabel alloc]initWithFrame:CGRectMake(20, 75, 130, 21)];
        player2Label.text = [NSString stringWithFormat:@"player 2 Score"];
        player2Label.tag = 43;
        [presentedView addSubview:player2Label];

        self.p1ScoreField = [[UITextField alloc]initWithFrame:CGRectMake(150, 55, 80, 21)];
        self.p1ScoreField.placeholder= @"score";
        self.p1ScoreField.tag = 44;
        self.p1ScoreField.keyboardType = UIKeyboardTypeNumberPad;
        self.p1ScoreField.borderStyle = UITextBorderStyleRoundedRect;
        self.p1ScoreField.delegate = self;
        [self.p1ScoreField addTarget:self action:@selector(UITextFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        [presentedView addSubview:self.p1ScoreField];

       self.p2ScoreField = [[UITextField alloc]initWithFrame:CGRectMake(150, 75, 80, 21)];
        self.p2ScoreField.tag = 45;
        self.p2ScoreField.placeholder = @"score";
        self.p2ScoreField.keyboardType = UIKeyboardTypeNumberPad;
        self.p2ScoreField.borderStyle = UITextBorderStyleRoundedRect;
        self.p2ScoreField.delegate = self;
        [self.p2ScoreField addTarget:self action:@selector(UITextFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        [presentedView addSubview:self.p2ScoreField];


    }

}

在单独的视图Controller中显示的视图。在viewDidAppear方法的alertView上调用Alloc / Init。

- (IBAction)addNewRoundButtonPressed:(id)sender {
    [alertView show];
}

1 个答案:

答案 0 :(得分:1)

Apple向<{1}}添加子视图不支持,强烈建议不要这样做。

最好使用带有自定义模式演示的常规视图控制器(查找UIAlertView)或Google搜索第三方替换。