automaticAdjustsScrollViewInsets不起作用

时间:2014-01-11 23:30:28

标签: ios objective-c ios7 uiscrollview uitabbar

我创建了一个非常简单的演示应用来测试automaticallyAdjustsScrollViewInsets的功能,但是我的标签栏覆盖了tableView的最后一个单元格。

我的AppDelegate代码:

UITabBarController *tabControl = [[UITabBarController alloc] init];
tabControl.tabBar.translucent = YES;
testViewController *test = [[testViewController alloc] init];
[tabControl setViewControllers:@[test]];

[self.window setRootViewController:tabControl];

我的testViewController(UITableViewController的子类)代码:

- (void)viewDidLoad
{
[super viewDidLoad];
self.automaticallyAdjustsScrollViewInsets = YES;
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
self.tableView.dataSource = self;
self.tableView.scrollIndicatorInsets = self.tableView.contentInset;
//[self.view addSubview:self.tableView];

// Do any additional setup after loading the view.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
cell.textLabel.text = @"test";
return cell;
}

这是iOS 7中的错误吗?如果没有,我做错了什么?

6 个答案:

答案 0 :(得分:54)

我认为automaticallyAdjustsScrollViewInsets仅在控制器viewUIScrollView时才有效(表格视图为1)。

您的问题似乎是您的控制器的view是常规的UIView而您的UITableView只是一个子视图,所以您将会这样做必须:

  • 让表格查看" root"图。

  • 手动调整插图:

    UIEdgeInsets insets = UIEdgeInsetsMake(controller.topLayoutGuide.length,
                                           0.0,
                                           controller.bottomLayoutGuide.length,
                                           0.0);
    scrollView.contentInset = insets;
    

修改

似乎SDK能够调整一些滚动视图,尽管它不是控制器的根视图。

到目前为止它适用于UIScrollViewUIWebView' s scrollView,因为它们是索引0的子视图。

无论如何,这可能会在未来的iOS版本中发生变化,因此您自己更安全地调整插图。

答案 1 :(得分:19)

您的视图控制器必须直接在UINavigaitonController的堆栈上才能使automaticallyAdjustsScrollViewInsets正常工作(即不是子视图控制器)

如果它是导航堆栈上另一个视图控制器的子视图控制器,则可以在父项上设置automaticallyAdjustsScrollViewInsets = NO。或者你可以这样做:

self.parentViewController.automaticallyAdjustsScrollViewInsets = NO;

答案 2 :(得分:15)

我知道这篇文章有点陈旧但是我刚用iOS 11和swift 4解决了这个问题,我当前的问题是iOS11有一个新的属性来验证ScrollView存在时的插图,一个是{{3这是ScrollView的属性,默认属性是automatic所以我的代码是:

if #available(iOS 11, *) {
    myScroll.contentInsetAdjustmentBehavior = .never
} else {
    self.automaticallyAdjustsScrollViewInsets = false
}

我希望这也解决你的问题......

答案 3 :(得分:2)

我有这个层次结构:

  1. 自定义navigationcontroller包含自定义tabbarcontroller

  2. 自定义tabbarcontroller包含多个控制器

  3. 这些控制器包含子视图,其中一个包含uiscrollview的子类。

  4. 我必须将automaticAdjustsScrollViewInsets设置为NO

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view from its nib.
    
        self.automaticallyAdjustsScrollViewInsets = NO;
    
    自定义tabbarcontroller 中的

    。层次结构中的其他控制器对嵌套滚动视图的行为没有任何影响。

答案 4 :(得分:2)

我遇到了同样的问题,一个带有不需要的顶部填充的表视图。

所有答案都说要通过设置automaticallyAdjustsScrollViewInsets = NO进行修复,但这并不能消除我的填充。

与此处的其他答案类似,如果您使用的是非标准视图层次结构,则需要稍微调整这些说明。

我有一个带有嵌入式UITableViewController的UIViewController。它无法在表视图控制器上设置automaticallyAdjustsScrollViewInsets

相反,我在嵌入我的表视图控制器的 UIViewController上设置了automaticallyAdjustsScrollViewInsets = NO。这成功地消除了表视图上的填充。

答案 5 :(得分:-4)

self.textView.contentInset = UIEdgeInsetsMake(1.0,0.0,0,0.0);
[self setEdgesForExtendedLayout:UIRectEdgeNone];