UIScrollView -setContentInset not working

喜欢而已 提交于 2019-12-13 21:14:38

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!