问题
I've got a fairly straightforward scroll view setup, and I want to get rid of the contentInset.
MyViewController.h
@interface MyViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@end
MyViewController.m
@interface MyViewController
-(void)viewDidLoad
{
[super viewDidLoad];
[self.scrollView setContentInset:UIEdgeInsetsZero];
...
}
In my storyboard, my scrollview starts from the bottom of the navbar. Unfortunately, I get the below result when running the app. Seems like the contentInset is still very much alive. I've made the scrollView background green.
EDIT: just providing a pic of the storyboard and the outlet link
回答1:
Ok, so the code
[self.myScrollViewOutlet setContentInset:UIEdgeInsetsZero];
worked only when placed in -(void)viewWillLayoutSubviews.
It did not work in -(void)viewDidLoad, -(void)viewWillAppear:(BOOL)animated or -(void)viewDidAppear:(BOOL)animated.
Hope that helps people in future!
回答2:
Swift 3
In your UIViewController which contains your WebView surprisingly named for example webView.
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews();
webView.scrollView.contentInset = UIEdgeInsets.zero;
}
来源:https://stackoverflow.com/questions/25714681/uiscrollview-setcontentinset-not-working