ios将子视图添加到不在中心的ScrollView

时间:2015-06-11 23:22:58

标签: ios objective-c uiview uiscrollview

我有一个iOS项目,可以为笔尖添加子视图。使用自动布局将子视图的内容设置为中心,但是当子视图添加到滚动视图时,子视图的内容不在中心。 这是代码:

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

此外输出如下: http://imgur.com/h6cejTA

我还上传了我的项目: http://www.filedropper.com/myproject

1 个答案:

答案 0 :(得分:0)

我解决了。这是我使用的代码:

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];
相关问题