【问题标题】:Scroll view and View goes up when key board appears iOS出现键盘时滚动视图和视图上升iOS
【发布时间】:2014-08-20 11:08:09
【问题描述】:

我正在使用故事板开发一个 iPad 应用程序。对于我的 iPad 应用程序,我需要在一个屏幕上显示 4 个UICollectionView。但是屏幕上没有足够的空间来显示所有 4 个UICollectionView,所以我需要向我的视图控制器添加一个滚动视图并在滚动视图中显示 CollectionViews。collectionView 中的每个单元格,所有集合视图都有一个文本字段。但是如果出现键盘,则第三个 CollectionView 和第四个 CollectionView 隐藏在后面键盘。所以我需要做的是在键盘出现时让 CollectionViews 和我的视图在滚动视图中。需要这方面的指导。

谢谢。

【问题讨论】:

  • 为键盘可见和隐藏通知添加观察者。并相应调整滚动视图的框架
  • @Suhail 我是 ios 新手。请您一步一步解释作为答案。

标签: ios objective-c ipad uiscrollview storyboard


【解决方案1】:

您将需要使用键盘通知。一个例子可能是这样的:

在你的viewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil]; //This gets triggered when the keyboard comes up. 

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil]; // This gets triggered when the keyboard goes down.

在你的代码中,你可以像这样实现处理滚动视图的方法:

-(void)keyboardWillShow:(NSNotification *)notification {

// Code to move the UIScrollView up when the keyboard comes up

}



-(void)keyboardWillHide:(NSNotification *)notification {

// Code to move the UIScrollView down when the keyboard goes down. 

}

编辑:

这在 tutorial 中有清楚的解释

【讨论】:

  • 谢谢你的帮助。但首先我需要设置一个滚动视图。但滚动视图不起作用。请帮我在故事板中设置它
【解决方案2】:
 - (void)registerForKeyboardNotifications
{
   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardIsShown:) name:UIKeyboardWillShowNotification object:nil];

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

- (void)keyboardIsShown:(NSNotification*)notification
{
         NSDictionary* info = [notification userInfo];
        CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    //Adjust your scrollview frame by reducing the height by keyboardSize.Height

}

- (void)keyboardWillBeHidden:(NSNotification*)notification
{
     // Set the scrollview frame back to normal.
}

【讨论】:

  • 在 viewDidLoad 方法中调用 registerForKeyboardNotifications。
  • 谢谢你的帮助。但首先我需要设置一个滚动视图。但滚动视图不起作用。请帮我在故事板中设置它
  • 只需拖放并设置委托并创建IBoutlet,即enf
  • 我错过了设置委托。现在也设置委托。但是如何在尺寸检查器中直接设置滚动视图的高度?
  • 这是大小检查器,在属性检查器旁边
猜你喜欢
  • 2012-02-01
  • 2012-06-29
  • 1970-01-01
  • 2011-09-02
  • 1970-01-01
  • 2015-11-29
  • 1970-01-01
  • 2018-05-21
相关资源
最近更新 更多