简体   繁体   English

键盘隐藏视图(AutoLayout + StoryBoard)

[英]Keypad hides View (AutoLayout + StoryBoard)

Apologies for this basic question, I am new to iOS development. 道歉,我是iOS开发的新手。 I have a UITextField in a View with AutoLayout that I would like to use to key in messages for a chat system. 我在具有AutoLayout的视图中有一个UITextField,我想用它来键入聊天系统的消息。 However when the keypad is displayed it hides the View containing the UITextField (the entire View is behind the keypad). 但是,显示小键盘时,它将隐藏包含UITextField的视图(整个视图位于小键盘的后面)。

What should be done to move the View along with the keypad when the keypad transitions from the bottom? 当键盘从底部过渡时,应该怎么做才能将视图与键盘一起移动? The UIView should be back in its original position (at the bottom of the screen) when the keypad is dismissed. 关闭键盘后,UIView应该回到其原始位置(在屏幕底部)。 My entire UI has been designed with AutoLayout in Storyboard. 我的整个UI都是使用Storyboard中的AutoLayout设计的。

Edit: I looked up How do I scroll the UIScrollView when the keyboard appears? 编辑:我抬起头,出现键盘时如何滚动UIScrollView? for a solution however there doesn't seem to be any indication AutoLayout has been used along constraints in this answer. 对于解决方案,似乎没有任何迹象表明已经在此答案中沿约束条件使用了自动布局。 How can the same be achieved using AutoLayout in Storyboard. 如何使用情节提要中的“自动布局”实现相同的目的。 Again, apologies for any lack of understanding as I am very new to iOS development. 再次道歉,对于任何缺乏理解的人,因为我是iOS开发的新手。

Add a UIScrollView to your view controller and keep your UITextField over scrollview. 将UIScrollView添加到视图控制器,并使UITextField保持在scrollview上。

add UITextFieldDelegate 添加UITextFieldDelegate

yourTextField.delegate = self;

you can set content offset of scrollview when touch on UITextField and reposition it to (0, 0) when keyboard resign. 您可以在触摸UITextField时设置scrollview的内容偏移,并在键盘退出时将其重新定位为(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;
} 

This is what I did recently, may be it helps. 这是我最近所做的,可能会有所帮助。

All my fields are inside a wrapper view with a top constraint. 我所有的字段都在具有顶部约束的包装视图内。 Since for me moving the wrapper view a few pixels up and down was enough I use this approach. 因为对我来说,将包装视图上下移动几个像素就足够了,所以我使用了这种方法。 Here is an example with a scroll view. 是带有滚动视图的示例。

I use a IBOutlet to reference this constraint 我使用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];
}

Then on the notification methods 然后关于通知方法

  #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;
        }
    }

There are tons of this kind of answers here on SO. 在SO上有很多这样的答案。 Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用自动布局时查看层次结构中的隐藏 - View hides in hierarchy when using autolayout 故事板Segue在目标视图上隐藏tabBarButton - Storyboard segue hides tabBarButton on destination view 使用故事板中的AutoLayout在表格视图单元格中的图像 - Image in Table View Cell using AutoLayout in Storyboard 在StoryBoard中为单个View Controller禁用自动布局 - Disabling AutoLayout for a single View Controller in storyBoard 使用代码而非情节提要添加的View自动布局(VFL) - Autolayout (VFL) for View that is added with code not storyboard 使用情节提要中的自动布局在景观中更改视框 - Change view frame in landscape using autolayout in storyboard 使用Storyboard或xib中的iOS AutoLayout和SizeClass在不同屏幕中可变的视图高度 - Variable height of view in different screens with iOS AutoLayout And SizeClass in Storyboard or xib 类似于UILabel的自动布局中的自定义视图的故事板设置宽度/高度 - Storyboard Set Width / Height for Custom View in Autolayout Similar to UILabel iOS /自动布局/故事板:引脚视图对表头无响应 - iOS/autolayout/storyboard: Pin view not responsive for table header 使用自动版式情节提要的图像视图更改iPhone屏幕尺寸的大小 - Image View change size for iPhone Screen Size using AutoLayout Storyboard
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM