出现键盘时iOS ViewController subview 移动
<p><p>我有一个 <code>UITableView</code>,它是我的 ViewController 的一个属性。此 ViewController 使用自动布局 +Storyboard约束位于屏幕顶部下方 40 像素处。我最近添加了一个标签和文本字段,它们最初隐藏在 Storyboard 中,并被限制在表格 View 的正后方。 </p>
<p>我添加了显示这些字段的方法:</p>
<p><code>-(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);
}
}];
}</code></p>
<p>这似乎工作正常,除非当我点击文本字段时,它将表格 View 移回隐藏文本字段上方。我不知道发生了什么。 <a href="/image/xHMyG.png" rel="noreferrer noopener nofollow"><img src="/image/xHMyG.png" alt="First State"/></a> <a href="/image/3WcOl.png" rel="noreferrer noopener nofollow"><img src="/image/3WcOl.png" alt="Second State"/></a> <a href="/image/YUvJ0.png" rel="noreferrer noopener nofollow"><img src="/image/YUvJ0.png" alt="Third State"/></a> </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>为了改变文本文件的高度,在你的 Controller 中声明 NSLayoutConstraint 变量约束。</p>
<p>在 .h 文件中:</p>
<pre><code> __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
</code></pre>
<p>在您的 Storyboard中
<a href="/image/etFZH.png" rel="noreferrer noopener nofollow"><img src="/image/etFZH.png" alt="enter image description here"/></a> </p>
<p>已连接</p>
<p> <a href="/image/7DPFo.png" rel="noreferrer noopener nofollow"><img src="/image/7DPFo.png" alt="Link variables"/></a> </p>
<p>使用约束改变框架。</p>
<pre><code>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;
}
</code></pre>
<p>您还可以注册键盘通知。</p>
<pre><code>- (void)registerForKeyboardNotifications
{
[ addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[ addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
</code></pre>
<p>并在通知回调中处理高度变化-</p>
<pre><code>@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
;
;
;
;
;
}
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
self.tableBottomSpaceConstrain.constant =0;
;
;
;
}
</code></pre>
<p>了解更多关于 NSLayoutConstraint
<a href="http://www.techotopia.com/index.php/Implementing_iOS_6_Auto_Layout_Constraints_in_Code" rel="noreferrer noopener nofollow">Implementing AutoLayout Constraints in Code</a> </p>
<p>见 <a href="https://developer.apple.com/library/ios/documentation/AppKit/Reference/NSLayoutConstraint_Class/" rel="noreferrer noopener nofollow">Apple Doc</a> </p>
<p>如果 textfield 是 tableview <a href="https://github.com/michaeltyson/TPKeyboardAvoiding" rel="noreferrer noopener nofollow">TPKeyboardAvoiding</a> 的一部分,您可以使用一些第三方代码</p>
<p>查看此链接:<a href="https://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present" rel="noreferrer noopener nofollow">How to make a UITextField move up when keyboard is present?</a> </p>
<p> <a href="https://stackoverflow.com/questions/594181/making-a-uitableview-scroll-when-text-field-is-selected" rel="noreferrer noopener nofollow">Making a UITableView scroll when text field is selected</a> </p></p>
<p style="font-size: 20px;">关于出现键盘时iOS ViewController subview 移动,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/34078563/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/34078563/
</a>
</p>
页:
[1]