IOS - Centering UIScrollView inside UIView container programmatically with Autolayout issue

我只是一个虾纸丫 提交于 2019-12-11 20:35:58

问题


I'm trying to replicate the safari pages scrollview, which has its frame that is slightly narrower than iPhone screen. So According to few suggestions I'm placing a the scrollview (450x280 points) inside a "clipView" (450x320 points).

//viewController methods
-(void)viewDidAppear:(BOOL)animated{

CGRect pagingScrollViewFrame = self.clipview.frame;
pagingScrollViewFrame.size.width = 280;

NSLog(@"clipview h:%f e w:%f",self.clipview.frame.size.height,self.clipview.frame.size.width);
NSLog(@"scrollview h:%f e w:%f",pagingScrollViewFrame.size.height,pagingScrollViewFrame.size.width);
NSLog(@"center clipview :%@",NSStringFromCGPoint(self.clipview.center));

pagingScrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame];
NSLog(@"center scrollview:%@",NSStringFromCGPoint(pagingScrollView.center));
pagingScrollView.contentSize = CGSizeMake(pagingScrollViewFrame.size.width *3, pagingScrollViewFrame.size.height);

pagingScrollView.clipsToBounds = NO;
pagingScrollView.center = self.clipview.center;

    [clipview addSubview:pagingScrollView];
}

As you can see I have my scrollView's center That is shifted 20points down ({140, 232}). My view Container has its center at {160, 232}.

Where am I wrong?There is anything like content offset property I should play with? is this issue related to Autolayout ?


回答1:


The issue is in this line:

pagingScrollView.center = self.clipview.center;

Should be:

pagingScrollView.center = CGPointMake(CGRectGetMidX(self.clipview.bounds), CGRectGetMidY(self.clipview.bounds));

It is common mistake to assign center of superview to its subview. They are in different coordinate systems.



来源:https://stackoverflow.com/questions/15636186/ios-centering-uiscrollview-inside-uiview-container-programmatically-with-autol

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