键盘出现时,ios无法更改子视图的位置

时间:2014-07-27 23:04:24

标签: ios position subview

我是iOS新手。我在屏幕中间使用故事板添加了一个带有标签= -125的textField,并将其链接到插座。我想在键盘出现时将其向上移动一点。

下面的代码将其背景颜色更改为绿色,但它不会改变其位置!

- (void)keyboardWillShow:(NSNotification *)notification
{
    [UIView animateWithDuration:0.3 animations:^{
        for (UIView *subView in self.view.subviews){
            if(subView.tag == -125){ //the tag of my textfield. using self.myTextField gave same results
                subView.backgroundColor = [UIColor greenColor];     //this works fine!!
                subView.frame = CGRectMake(subView.frame.origin.x, subView.frame.origin.y - 50, subView.frame.size.width, subView.frame.size.height);       //this does not work!! does not move!
            }
        }
    }];
}


//i don't do anything now in keyboardWillHide


- (void)viewWillAppear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}


- (void)viewWillDisappear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

我已将其定位在屏幕中间,以查看键盘启动时其位置是否发生变化。它没有改变。背景变为绿色!

0 个答案:

没有答案