ios 自动调整滚动视图插入不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21069258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:05:05  来源:igfitidea点击:

automaticallyAdjustsScrollViewInsets not working

iosobjective-cios7uiscrollviewuitabbar

提问by danielmhanover

I've created an extremely simple demo app to test the functionality of automaticallyAdjustsScrollViewInsets, but the last cell of the tableView is covered by my tab bar.

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

My AppDelegate code:

我的 AppDelegate 代码:

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

[self.window setRootViewController:tabControl];

My testViewController (subclass of UITableViewController) Code:

我的 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;
}

Is this a bug in iOS 7? If not, what did I do wrong?

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

回答by Rivera

I think that automaticallyAdjustsScrollViewInsetsonly works when your controllers viewis a UIScrollView(a table view is one).

我认为automaticallyAdjustsScrollViewInsets只有当您的控制器view是一个UIScrollView(表视图是一个)时才有效。

You're problem seems to be that your controller's viewis a regular UIViewand your UITableViewis just a subview, so you'll have to either:

您的问题似乎是您的控制器view是常规的,UIView而您UITableView的只是一个子视图,因此您必须:

  • Make the table view the "root" view.

  • Adjust insets manually:

    UIEdgeInsets insets = UIEdgeInsetsMake(controller.topLayoutGuide.length,
                                           0.0,
                                           controller.bottomLayoutGuide.length,
                                           0.0);
    scrollView.contentInset = insets;
    
  • 使表视图成为“根”视图。

  • 手动调整插图:

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

Edit:

编辑:

Seems like the SDK is capable of adjusting some scroll views despite not being the controller's root view.

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

So far It works with UIScrollView's and UIWebView's scrollViewwhen they are the subview at index 0.

到目前为止,当它们是 index 的子视图时,它可以与UIScrollView's 和UIWebView'sscrollView一起使用0

Anyway this may change in future iOS releases, so you're safer adjusting insets yourself.

无论如何,这可能会在未来的 iOS 版本中改变,所以你自己调整插图会更安全。

回答by Robert

Your view controller must be directly on a UINavigaitonController's stack for automaticallyAdjustsScrollViewInsetsto work (i.e. not a child view controller)

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

If it is a child view controller of another view controller which is on the navigation stack, you can instead set automaticallyAdjustsScrollViewInsets = NOon the parent. Alternatively you can do this:

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

self.parentViewController.automaticallyAdjustsScrollViewInsets = NO;

回答by DariusV

I just solved this issue with iOS 11 and swift 4, my current problem was that iOS11 has a new property to validate the insets when a ScrollView does exist, that one is contentInsetAdjustmentBehaviorwhich is a ScrollView's property and the default property is automaticso my code was:

我刚刚用 iOS 11 和 swift 4 解决了这个问题,我目前的问题是当 ScrollView 确实存在时,iOS11 有一个新属性来验证插入,那个contentInsetAdjustmentBehavior是 ScrollView 的属性,默认属性是automatic这样我的代码是:

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

I hope this solve your problems too...

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

回答by pkamb

I was having the same issue, a Table View with unwanted top padding.

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

All answers say to fix by setting automaticallyAdjustsScrollViewInsets = NO, but that was not eliminating the padding for me.

所有答案都说通过设置来修复automaticallyAdjustsScrollViewInsets = NO,但这并没有消除我的填充。

Similar to the other answers here, these directions need to be tweaked slightly if you're using a non-standard view hierarchy.

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

I had a UIViewController with an embedded UITableViewController. It was not working to set automaticallyAdjustsScrollViewInsetson the Table View Controller.

我有一个带有嵌入式 UITableViewController 的 UIViewController。automaticallyAdjustsScrollViewInsets在表视图控制器上设置不起作用。

Instead, I set automaticallyAdjustsScrollViewInsets = NOon the parentUIViewController that was embedding my Table View Controller. That successfully eliminated the padding on the Table View.

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

回答by Vladimír Slavík

I have this hierarchy:

我有这个层次结构:

  1. custom navigationcontroller contains custom tabbarcontroller

  2. custom tabbarcontroller contains several controllers

  3. these controllers contains subviews and one of them contains a subclass of uiscrollview.

  1. 自定义导航控制器包含自定义 tabbarcontroller

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

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

I had to set automaticallyAdjustsScrollViewInsets to NO

我不得不将automaticAdjustsScrollViewInsets设置为NO

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    self.automaticallyAdjustsScrollViewInsets = NO;

in the custom tabbarcontroller. Other controllers in the hierarchy do not have any impact on the nested scroll view's behavior.

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