ios Adding SubView to a ScrollView not on center

1.3k Views Asked by At

I have an iOS project that adds a subview to a nib. The subview's contents are set to the center using autolayout, but when the subview is added to the scrollview, the subview's contents are not on center. Here is the code:

UIView *scroller = [[[NSBundle mainBundle] loadNibNamed:@"scroller" owner:self options:nil] objectAtIndex:0];
[self.view addSubview:scroller];

CGSize size = self.view.bounds.size;
NSLog(@"view size: %f", size.width);
self.scrollView.contentSize = CGSizeMake(size.width, size.height);
self.scrollView.frame = CGRectMake(0, 0, size.width, size.height);
self.myView.frame = CGRectMake(0, 0, size.width, size.height);
self.scrollView.bounds = self.myView.bounds;
[self.scrollView addSubview:self.myView];

Also here is what the output looks like: https://i.stack.imgur.com/1RxYh.jpg

I have also uploaded my project: http://www.filedropper.com/myproject

1

There are 1 best solutions below

0
On BEST ANSWER

I solved it. Here's the code I used:

UIView *scroller = [[[NSBundle mainBundle] loadNibNamed:@"scroller" owner:self options:nil] objectAtIndex:0];
[self.view addSubview:scroller];

CGSize size = self.view.bounds.size;
self.scrollView.contentSize = CGSizeMake(size.width, size.height+200);
self.myView.frame = CGRectMake(0, 0, size.width, size.height);
[self.scrollView addSubview:self.myView];