9438

IOS - Centering UIScrollView inside UIView container programmatically with Autolayout issue

Question:

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]; }

<img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/gNAd7.png" data-original="https://i.stack.imgur.com/gNAd7.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

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

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

Answer1:

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.

Recommend

  • How to make UIViewController re-load the view?
  • How to send the details of a selected cell to another view controller
  • TableView not reloading
  • Update UITextView during recursive function is running
  • Why does my time UILabels skip 2 seconds when it resumes play from a pause state?
  • Passing one to many core data between view controllers
  • AMScrollingNavbar does not work. Nothing happens
  • pushViewController stuck or viewdidappear fail
  • data from camera video returning zero
  • data from camera video returning zero
  • Autolayout is not updating frames instantly in iOS8-Xcode6
  • Change height constraint of table view outside of viewDidAppear in iOS 7
  • alert with UIAlertController can't dissmiss it
  • View Animation does not work - ios Swift 3
  • Opengl Iphone SDK: How to tell if you're touching an object on screen?
  • Dynamically change cell's height while typing text, and reload the containing tableview for res
  • MonoTouch SIGABRT “Ran out of trampolines of type 2” error
  • UITableView gives empty table, does not load data
  • dismissModalViewControllerAnimated nonatomic?
  • Auto Height of UICollectionView inside UITableViewCell
  • “[CALayer release]: message sent to deallocated instance” when dismissing modal view controller
  • iOS 9 errors and correct conversion to swift 2
  • Generate a unique string based on a pair of strings
  • “A GKScore must specify a leaderboard.”
  • Deleting a widget from QTableView
  • Jquery UI Sortable, move item automatically
  • multidatatrigger with multibinding in ControlTemplate.Triggers
  • Is there a parser equivalent of 'fragment' marking in ANTLR4?
  • Build Successful but not running on simulator
  • custom UITableViewCell with image for highlighting
  • Azure Cloud Service Web Role web pages do not load
  • swift auto completion not working in Xcode6-Beta
  • Finding past revisions of files in StarTeam w/ .NET SDK / C#
  • Cannot Parse HTML Data Using Android / JSOUP
  • C# - Getting references of reference
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?