【问题标题】:Keypad hides View (AutoLayout + StoryBoard)键盘隐藏视图(AutoLayout + StoryBoard)
【发布时间】:2015-09-16 12:30:25
【问题描述】:

对于这个基本问题,我很抱歉,我是 iOS 开发的新手。我在带有 AutoLayout 的视图中有一个 UITextField,我想用它来键入聊天系统的消息。但是,当显示键盘时,它会隐藏包含 UITextField 的视图(整个视图都在键盘后面)。

当键盘从底部转换时,应该怎么做才能将视图与键盘一起移动?当键盘被关闭时,UIView 应该回到原来的位置(在屏幕底部)。 我的整个 UI 都是使用 Storyboard 中的 AutoLayout 设计的。

编辑: 我查找了How do I scroll the UIScrollView when the keyboard appears? 的解决方案,但似乎没有任何迹象表明 AutoLayout 已在此答案中沿约束条件使用。如何在 Storyboard 中使用 AutoLayout 来实现相同的效果。再次对任何缺乏理解表示歉意,因为我对 iOS 开发非常陌生。

【问题讨论】:

标签: ios autolayout xcode-storyboard


【解决方案1】:

将 UIScrollView 添加到您的视图控制器,并将您的 UITextField 保留在滚动视图上。

添加 UITextFieldDelegate

yourTextField.delegate = self;

您可以在触摸 UITextField 时设置滚动视图的内容偏移量,并在键盘退出时将其重新定位为 (0, 0)。

-(void)viewWillAppear:(BOOL)animated
{
yourScrollView.contentSize = CGSizeMake(320, 500);

[super viewWillAppear:YES];

}

-(void)textFieldDidBeginEditing:(FMTextField *)textField
{
    [yourScrollView setContentOffset:CGPointMake(0,textField.center.y-140) animated:YES];//you can set your  y cordinate as your req also
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
     [textField resignFirstResponder];
     [yourScrollView setContentOffset:CGPointMake(0,0) animated:YES];
    return YES;
} 

【讨论】:

  • 感谢您的回答。是否已尝试使用 AutoLayout 约束?
  • 更改滚动视图的contentOffset 与自动布局无关
【解决方案2】:

这是我最近做的,可能有帮助。

我所有的字段都在一个带有顶部约束的包装视图中。因为对我来说将包装视图上下移动几个像素就足够了,所以我使用了这种方法。 Here 是一个带有滚动视图的示例。

我使用 IBOutlet 来引用这个约束

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

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //register for keyboard notifications
    _keyboardIsShowing = NO;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

然后是通知方法

  #pragma mark - Keyboard notifications methods
    - (void)keyboardWillShow:(NSNotification *) notification{

        if (!_keyboardIsShowing) {
            _keyboardIsShowing = YES;
            [UIView animateWithDuration:0.4 animations:^{
//here update the top constraint to some new value
                _topConstraint.constant = _topConstraint.constant - 30;
                [self.view layoutIfNeeded];
            }];
        }
    }

    - (void)keyboardWillHide:(NSNotification *) notification{
        if (_keyboardIsShowing) {
            [UIView animateWithDuration:0.4 animations:^{
                _topConstraint.constant = _topConstraint.constant + 30;
                [self.view layoutIfNeeded];
            }];

            _keyboardIsShowing = NO;
        }
    }

在 SO 上有很多这样的答案。 祝你好运。

【讨论】:

    猜你喜欢
    • 2014-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-08
    • 2013-01-22
    • 2018-06-19
    • 1970-01-01
    相关资源
    最近更新 更多