我有一个 UITableView
,它是我的 View Controller 的一个属性。此 View Controller 使用自动布局 + Storyboard约束位于屏幕顶部下方 40 像素处。我最近添加了一个标签和文本字段,它们最初隐藏在 Storyboard 中,并被限制在表格 View 的正后方。
我添加了显示这些字段的方法:
-(void)showNeighborhoodFilter {
[UIView animateWithDuration:.5 动画:^{
如果(self.neighborhoodLabel.hidden){
self.neighborhoodLabel.hidden=否;
self.neighborhoodSelector.hidden=NO;
self.tableView.frame = CGRectMake(0, self.tableView.frame.origin.y+40, self.tableView.frame.size.width, self.tableView.frame.size.height-40);
}
别的{
self.neighborhoodLabel.hidden=YES;
self.neighborhoodSelector.hidden=YES;
self.tableView.frame = CGRectMake(0, self.tableView.frame.origin.y-40, self.tableView.frame.size.width, self.tableView.frame.size.height+40);
}
}];
}
这似乎工作正常,除非当我点击文本字段时,它将表格 View 移回隐藏文本字段上方。我不知道发生了什么。
为了改变文本文件的高度,在你的 Controller 中声明 NSLayoutConstraint 变量约束。
在 .h 文件中:
__weak IBOutlet NSLayoutConstraint *heightConstraintOfMyView;//height of the view
__weak IBOutlet NSLayoutConstraint *verticalspaceConstraintOfMyViewWithTopView;//space between myview and the view just above it
__weak IBOutlet NSLayoutConstraint *verticalSpaceConstraintOfMyViewWithBottomView;//space between myview and the view just below it
已连接
使用约束改变框架。
if (myConditionTrueForShowingView) {
//setting the constraint to update height
heightConstraintOfMyView = 40;
verticalspaceConstraintOfMyViewWithTopView.constant=20;
verticalSpaceConstraintOfMyViewWithBottomView.constant=80;
}
else{
//setting the constraint to update height
heightConstraintOfMyView = 0;
verticalspaceConstraintOfMyViewWithTopView.constant=20;
verticalSpaceConstraintOfMyViewWithBottomView.constant=0;
}
您还可以注册键盘通知。
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selectorselector(keyboardWasShown
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selectorselector(keyboardWillBeHidden
name:UIKeyboardWillHideNotification object:nil];
}
并在通知回调中处理高度变化-
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableBottomSpaceConstrain;
-(void)keyboardWasShownNSNotification*)aNotification
{
self.view.translatesAutoresizingMaskIntoConstraints = YES;
if (self.tableBottomSpaceConstrain.constant) {//
return;
}
self.tableBottomSpaceConstrain.constant = 216;// Default Keyboard height is 216, varies for third party keyboard
[self.view setNeedsUpdateConstraints];
[self.view updateConstraints];
[self.view layoutIfNeeded];
[self.view setNeedsLayout];
[self.view setNeedsUpdateConstraints];
}
- (void)keyboardWillBeHiddenNSNotification*)aNotification
{
self.tableBottomSpaceConstrain.constant =0;
[self.view layoutIfNeeded];
[self.view setNeedsLayout];
[self.view updateConstraints];
}
了解更多关于 NSLayoutConstraint Implementing AutoLayout Constraints in Code
如果 textfield 是 tableview TPKeyboardAvoiding 的一部分,您可以使用一些第三方代码
查看此链接:How to make a UITextField move up when keyboard is present?
Making a UITableView scroll when text field is selected
关于出现键盘时iOS ViewController subview 移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34078563/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |