自动调整ScrollViewInsets不起作用
我创build了一个非常简单的演示应用程序来testingautomaticallyAdjustsScrollViewInsets
的function,但是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中的错误吗? 如果没有,我做错了什么?
我认为,只有当你的控制器view
是一个UIScrollView
(一个表视图是一个)时, automaticallyAdjustsScrollViewInsets
UIScrollView
才有效。
你的问题似乎是你的控制器的view
是一个普通的UIView
和你的UITableView
只是一个子视图,所以你必须要么:
-
使表查看“根”视图。
-
手动调整插入:
UIEdgeInsets insets = UIEdgeInsetsMake(controller.topLayoutGuide.length, 0.0, controller.bottomLayoutGuide.length, 0.0); scrollView.contentInset = insets;
编辑:
看起来像SDK能够调整一些滚动视图,尽pipe不是控制器的根视图。
到目前为止它可以在UIScrollView
和UIWebView
的scrollView
作为索引0
的子视图时使用。
无论如何,这可能会改变在未来的iOS版本,所以你更安全地调整自己插入。
您的视图控制器必须直接在UINavigaitonController的堆栈上automaticallyAdjustsScrollViewInsets
调用ScrollViewInsets(即不是子视图控制器)
如果它是位于导航堆栈上的另一个视图控制器的子视图控制器,则可以在父级上automaticallyAdjustsScrollViewInsets = NO
设置“ automaticallyAdjustsScrollViewInsets = NO
”。 或者你可以这样做:
self.parentViewController.automaticallyAdjustsScrollViewInsets = NO;
我有同样的问题,表视图与不需要的顶部填充。
所有答案说,通过设置automaticallyAdjustsScrollViewInsets = NO
来解决,但这并没有消除我的填充。
与其他答案类似,如果使用非标准视图层次结构,则需要稍微调整这些方向。
我有一个embeddedUITableViewController的UIViewController。 它不能在Table View Controller上automaticallyAdjustsScrollViewInsets
设置AdjustsScrollViewInsets。
相反,我设置automaticallyAdjustsScrollViewInsets = NO
的AdjustsScrollViewInsets automaticallyAdjustsScrollViewInsets = NO
embedded我的表视图控制器的父 UIViewController。 这成功地消除了表视图上的填充。
我知道这个post是有点老,但我刚刚解决了这个问题与iOS 11和Swift 4,我目前的问题是,iOS11有一个新的属性来validation当一个ScrollView存在的插入,一个是contentInsetAdjustmentBehavior
这是一个ScrollView的属性和默认属性是automatic
所以我的代码是:
if #available(iOS 11, *) { myScroll.contentInsetAdjustmentBehavior = .never } else { self.automaticallyAdjustsScrollViewInsets = NO; }
我希望这也能解决你的问题
我有这个层次结构:
-
自定义导航控制器包含自定义tabbarcontroller
-
自定义tabbarcontroller包含几个控制器
-
这些控制器包含子视图,其中一个包含uiscrollview的子类。
我不得不自动设置调整ScrollViewInsets为NO
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. self.automaticallyAdjustsScrollViewInsets = NO;
在自定义tabbarcontroller 。 层次结构中的其他控制器对嵌套滚动视图的行为没有任何影响。
self.textView.contentInset = UIEdgeInsetsMake(1.0,0.0,0,0.0); [self setEdgesForExtendedLayout:UIRectEdgeNone];