【问题标题】:How to avoid collision between UITextField and Keyboard using auto layout?如何使用自动布局避免 UITextField 和键盘之间的冲突?
【发布时间】:2016-03-03 09:40:57
【问题描述】:

1.我不喜欢使用任何第三方框架来做,只有约束有没有可能?

好吧,我试过了 :)

使用约束 IBOutlet 我需要更新它..

-(void)keyboardDidShow:(NSNotification*)aNotification{
    NSDictionary* info = [aNotification userInfo];

    double animationduration =[info[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    CGRect keyboardEndFrame =[info[UIKeyboardFrameEndUserInfoKey] CGRectValue];

    NSLog(@"this is user info notification keyboard did show==%@",info);
}

指导我的朋友们实现这一目标:)

【问题讨论】:

标签: ios objective-c nslayoutconstraint uikeyboard


【解决方案1】:

按照以下步骤操作:

  • 每当键盘出现时,只需更改插座的常量 根据您的需要进行约束。
  • 然后调用[self updateConstraintIfNeeded]更新约束
  • 然后调用[self layoutIfNeeded]更新视图布局
  • 每当键盘按下时,将常量更改回 原创的
  • 再次关注 3 和 4

提示:
在 UIView 动画块中调用布局更新调用将帮助您平滑过渡。

如果你想获得键盘尺寸:

- (void)keyboardWillShow:(NSNotification*)notification {
   NSDictionary *info = [notification userInfo];
   CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
   CGFloat deltaHeight = kbSize.height - _currentKeyboardHeight; 
   // Write code to adjust views accordingly using deltaHeight
   _currentKeyboardHeight = kbSize.height;

}

就是这样。

【讨论】:

  • 你能告诉我键盘尺寸吗?
【解决方案2】:

您必须更改NSLayoutConstraint 中的constant 属性。然后你必须刷新布局。

如果您的 IBOutlet 是:

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

NSLayoutConstraint 中的初始常量值为bottomInitialValue

代码应该是:

- (void) keyboardDidShow:(NSNotification*) aNotification
{
    NSDictionary* info = [aNotification userInfo];

    double animationduration =[info[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    CGRect keyboardEndFrame =[info[UIKeyboardFrameEndUserInfoKey] CGRectValue];

    bottomConstraint.constant = keyboardEndFrame.size.height + bottomInitialValue;
    [self.view layoutIfNeeded];
}

当键盘消失时,您必须将constant属性设置为初始值:

bottomConstraint.constant = bottomInitialValue;
[self.view layoutIfNeeded];

【讨论】:

    【解决方案3】:

    获取 imageView 顶部约束 IBOutlet

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

    向上

    - (void)keyboardWillShow:(NSNotification*)notification
     {
        NSDictionary *info = [notification userInfo];
        CGSize kbHeight = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
        constraintImageViewTop.constant = constraintImageViewTop.constant - kbSize.height;
     }
    

    向下

    - (void)keyboardWillHide:(NSNotification *)notification
     {
        NSDictionary *info = [notification userInfo];
        CGSize kbHeight = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
        constraintImageViewTop.constant = constraintImageViewTop.constant + kbSize.height;
     }
    

    使 PhoneNumber(textfield) 底部约束优先级低

    【讨论】:

      猜你喜欢
      • 2016-04-10
      • 2014-10-09
      • 1970-01-01
      • 1970-01-01
      • 2019-02-21
      • 2017-01-23
      • 1970-01-01
      • 2012-09-25
      • 1970-01-01
      相关资源
      最近更新 更多