显示键盘时UIViewController宽度更改

时间:2017-02-24 14:46:43

标签: ios objective-c uiviewcontroller keyboard xib

我有一个用其xib文件创建的exampleUIViewController。 mainView包含一些textFields。单击textField(开始编辑)时,我会显示带有动画的键盘:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {

self.view.frame = self.initialFrame;

 [textField resignFirstResponder];

    [UIView animateWithDuration:0.25 animations:^
     {
         CGRect newFrame = [view frame];
         newFrame.origin.y -= PORTRAIT_KEYBOARD_HEIGHT;
         [view setFrame:newFrame];

     }completion:^(BOOL finished)
     {

     }];
 return YES;
}

然后在textFieldShouldReturn

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {

self.view.frame = self.initialFrame;
[textField resignFirstResponder];

    [UIView animateWithDuration:0.25 animations:^
     {
         CGRect newFrame = [view frame];
         newFrame.origin.y += PORTRAIT_KEYBOARD_HEIGHT;
         [view setFrame:newFrame];

     }completion:^(BOOL finished)
     {

     }];

return YES;
}

viewDidLoad我将self.initialFrame设置为:

self.intialFrame = self.view.frame;

问题是当显示键盘时,mainView宽度会发生变化并变大。

我做了什么错了? 为什么我只有xib这个问题而不是UIViewController中创建的storyboard? 我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

在viewDidLoad()处无法获得正确的帧大小,请尝试在viewDidAppear()

答案 1 :(得分:0)

如果您想使用xib或storyboard ViewController获取正确的帧视图。不要使用

获取视图
<button type="button" data-mixitup-control data-filter=".red">Red</button>

获得正确框架的最佳方法是

- (void)viewDidLoad {
    [super viewDidLoad];
}