【发布时间】:2015-10-30 00:48:42
【问题描述】:
我没有找到一种方法来创建一个带有一些视图的滚动视图,我创建了一个带有一些元素和一些约束的 xib,我创建了一个带有 scrollview->scrollviewcontent->{v1,v2,v3} 机器人的控制器我的 v1,v2,v3 似乎受到约束。 这是我的代码,我哪里出错了?
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
if (isFirst)
{
[self initViews];
[self initConstraints];
[self initTutorial];
isFirst = NO;
}
}
-(void) initViews
{
_scrollView = [[UIScrollView alloc] init];
[_scrollView setPagingEnabled:YES];
[_scrollView setShowsHorizontalScrollIndicator:NO];
[_scrollView setBackgroundColor:[UIColor clearColor]];
[_scrollView setDelegate:self];
// ------------------------------------------------------------------
// This content view will be the only child view of scrollview
// ------------------------------------------------------------------
_scrollViewContent = [[UIView alloc] init];
[_scrollViewContent setBackgroundColor:[UIColor clearColor]];
// add content view to scrollview now
[_scrollView addSubview:_scrollViewContent];
// add scrollview to main view
[self.view addSubview:self.scrollView];
}
-(void) initConstraints
{
_scrollView.translatesAutoresizingMaskIntoConstraints = NO;
_scrollViewContent.translatesAutoresizingMaskIntoConstraints = NO;
id views = @{
@"_scrollView": _scrollView,
@"_scrollViewContent": _scrollViewContent
};
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_scrollView]|" options:0 metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_scrollView]|" options:0 metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_scrollViewContent]|" options:0 metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_scrollViewContent]|" options:0 metrics:nil views:views]];
}
- (void) initTutorial {
[self.view layoutIfNeeded];
int i = 0;
for (V10Page *page in pagineTutorial)
{
V10Widget *widget = [page.page objectForKey:@"description"];
TutorialPage *tutorial = [[TutorialPage alloc] initWithFrame:CGRectMake(i * [Utils getScreenSize].width, 0,[Utils getScreenSize].width, _scrollView.frame.size.height)
andText:[widget getValueForDefaultLanguage]
andImage:nil];
[_scrollViewContent addSubview:tutorial];
[self.view layoutIfNeeded];
i++;
}
}
- (void) viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
if (pagineTutorial.count > 0)
{
[_scrollViewContent setFrame:CGRectMake(0, 0, _scrollView.frame.size.width * pagineTutorial.count, _scrollView.frame.size.height)];
[_scrollView setContentSize:_scrollViewContent.frame.size];
}
[self.view layoutIfNeeded];
}
【问题讨论】:
标签: ios uiscrollview autolayout constraints xib