可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
My AppDelegate code:
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:
- (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?
回答1:
I think that automaticallyAdjustsScrollViewInsets
only works when your controllers view
is a UIScrollView
(a table view is one).
You're problem seems to be that your controller's view
is a regular UIView
and your UITableView
is just a subview, so you'll have to either:
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;
Edit:
Seems like the SDK is capable of adjusting some scroll views despite not being the controller's root view.
So far It works with UIScrollView
's and UIWebView
's scrollView
when they are the subview at index 0
.
Anyway this may change in future iOS releases, so you're safer adjusting insets yourself.
回答2:
Your view controller must be directly on a UINavigaitonController's stack for automaticallyAdjustsScrollViewInsets
to work (i.e. not a child view controller)
If it is a child view controller of another view controller which is on the navigation stack, you can instead set automaticallyAdjustsScrollViewInsets = NO
on the parent. Alternatively you can do this:
self.parentViewController.automaticallyAdjustsScrollViewInsets = NO;
回答3:
I know this post is a little old but 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 contentInsetAdjustmentBehavior
which is a ScrollView's property and the default property is automatic
so my code was:
if #available(iOS 11, *) {
myScroll.contentInsetAdjustmentBehavior = .never
} else {
self.automaticallyAdjustsScrollViewInsets = false
}
I hope this solve your problems too...
回答4:
I have this hierarchy:
custom navigationcontroller contains custom tabbarcontroller
custom tabbarcontroller contains several controllers
these controllers contains subviews and one of them contains a subclass of uiscrollview.
I had to set automaticallyAdjustsScrollViewInsets to 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.
回答5:
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.
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 automaticallyAdjustsScrollViewInsets
on the Table View Controller.
Instead, I set automaticallyAdjustsScrollViewInsets = NO
on the parent UIViewController that was embedding my Table View Controller. That successfully eliminated the padding on the Table View.
回答6:
self.textView.contentInset = UIEdgeInsetsMake(1.0,0.0,0,0.0);
[self setEdgesForExtendedLayout:UIRectEdgeNone];