gpt4 book ai didi

iOS ViewController subview 在键盘出现时移动

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:11:04 26 4
gpt4 key购买 nike

我有一个 UITableView,它是我的 View Controller 的一个属性。这个 View Controller 使用自动布局+ Storyboard约束定位在屏幕顶部下方 40 像素处。我最近添加了一个标签和文本字段,它们最初隐藏在 Storyboard中,并被限制在 tableview 的正后方。

我添加了一个方法来显示这些字段:

-(void)showNeighborhoodFilter {
[UIView animateWithDuration:.5 动画:^{
如果(self.neighborhoodLabel.hidden){
self.neighborhoodLabel.hidden=否;
self.neighborhoodSelector.hidden=否;
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);
}
}];
}

这似乎工作正常,除了当我点击文本字段时,它会将 tableview 移回隐藏的文本字段上方。我不知道发生了什么事。 First State Second State Third State

最佳答案

要更改文本字段高度,请在 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

在你的 Storyboard中 enter image description here

已连接

Link variables

使用约束改变框架。

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
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];

}

并处理通知回调中的高度变化-

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableBottomSpaceConstrain;

-(void)keyboardWasShown:(NSNotification*)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)keyboardWillBeHidden:(NSNotification*)aNotification
{
self.tableBottomSpaceConstrain.constant =0;
[self.view layoutIfNeeded];
[self.view setNeedsLayout];
[self.view updateConstraints];
}

了解更多关于 NSLayoutConstraint Implementing AutoLayout Constraints in Code

参见 Apple Doc

如果文本字段是表格 View 的一部分,您可以使用一些第三方代码 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com