似乎你有两个问题。首先是“对于较小的屏幕尺寸,一些元素正在消失”。那个由@Dalton Sweeney回答。使用自动布局然后重置建议约束
我要回答你的第二个问题,你应该做的事情时,键盘盖一些UIElemnts。
您不需要更改视图控制器,您应该在键盘隐藏/显示时动态更改视图的框架。我做在GitHub上演示,查看完整版,如果你想:https://github.com/EricZhang90/IOS_Demo/tree/master/KeyboadDemo
主要工作流程如下:
1)创建两个通知检测键盘即将隐藏/显示:
- (void)viewDidLoad {
[super viewDidLoad];
NSNotificationCenter *ctr = [NSNotificationCenter defaultCenter];
[ctr addObserver:self
selector:@selector(moveKeyboardInResponseToWillShowNotification:)
name:UIKeyboardWillShowNotification
object:nil];
[ctr addObserver:self
selector:@selector(moveKeyboardInResponseToWillHideNotification:)
name:UIKeyboardWillHideNotification
object:nil];
}
2)调整视图的框架
该方法有两个参数:第一个是从通知中心发送的键盘信息。你可以从第一步的方法中获得它。第二个参数表示此调整即将减少或扩大帧。当键盘即将隐藏时,它的值为0,并且当键盘即将出现时,它是键盘的框架。
-(void)adjustView: (NSDictionary *)info :(CGRect)rect{
// get duration of keyboard appears/hides
CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
// get keyboard's curve
UIViewAnimationCurve curve = [[info objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
[self.view layoutSubviews];
// Start to adjust frame:
// Animate:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:curve];
[UIView setAnimationBeginsFromCurrentState:YES];
// self.buttonLayoutConstraint is the layout constraint between bottom of view and bottom of screen(super view).
// This is the most important part.
self.buttonLayoutConstraint.constant = rect.size.height;
[self.view layoutSubviews];
[UIView commitAnimations];
}
“是否有方法只在需要时启用滚动视图?”这是默认行为。只有当其内容大于其视图时才滚动。看看[教程](http://www.ios-blog.co.uk/tutorials/objective-c/uitextfield-move-up-when-keyboard-present/)。 – shallowThought