UIScrollView contentInset不起作用

Dan*_*552 5 iphone objective-c uiscrollview uikit ios

我遇到的问题是contentInset不能使用UIScrollView来使用键盘弹出窗口.它有点工作:由于某种原因,我需要大数(可能高于视图的高度?)它可以做任何事情,尽管所有文档都contentInset显示小数字40.0(如条形码)或键盘高度.

我通过以下步骤在一个全新的应用程序上重现了这个问题:

  1. 使用Xcode新项目创建新的单一视图应用程序
  2. 在故事板上,拖动滚动视图,填充完整视图大小
  3. 在故事板上,拖动按钮,位于屏幕的最底部(在滚动视图内)
  4. 将scrollview链接到ViewController中的新属性
  5. 将按钮链接到ViewController中的方法
  6. 按下按钮的方法设置contentInset

以下是ViewController的代码:

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@end

@implementation ViewController
- (IBAction)button:(id)sender {
    self.scrollView.contentInset = UIEdgeInsetsMake(0.0, 0.0, 600, 0.0);
}
@end
Run Code Online (Sandbox Code Playgroud)

我缺少什么/为什么我需要大数字?

Mat*_*att 14

检查self.scrollView.contentSize设置是否正确.如果contentSize.height为0(在您的步骤之后将是这种情况),则需要大的插入.

尝试添加[self.scrollView setContentSize: CGSizeMake(320, 568)];按钮方法,您会注意到您的插入现在将按预期运行.