Ios 自动布局滚动视图最佳方法
我没有找到一种方法来创建一个包含一些视图的scrollview,我创建了一个包含一些元素和一些约束的xib,我使用scrollview->scrollviewcontent->{v1,v2,v3}创建了一个控制器,而我的v1,v2,v3似乎带有约束。 这是我的密码,我在哪里出错Ios 自动布局滚动视图最佳方法,ios,uiscrollview,autolayout,constraints,xib,Ios,Uiscrollview,Autolayout,Constraints,Xib,我没有找到一种方法来创建一个包含一些视图的scrollview,我创建了一个包含一些元素和一些约束的xib,我使用scrollview->scrollviewcontent->{v1,v2,v3}创建了一个控制器,而我的v1,v2,v3似乎带有约束。 这是我的密码,我在哪里出错 - (void) viewWillAppear:(BOOL)animated { [super viewWillAppear:YES]; if (isFirst) { [sel
- (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开发人员在将AutoLayout与UIScrollView一起使用时都有一个常见问题。这通常不是那么难,只是需要记住的几件事 将滚动视图添加到主UIView,并给出有关UIView的约束。 在ScrollView中添加内容视图UIView,并提供顶部、底部、左侧和右侧约束w.r.t ScrollView。还可以添加等宽和等高约束w.r.t main UIView。 如果需要垂直滚动,请将等高约束的优先级设置为低。 现在,您可以在内容视图中添加任何内容,并添加约束w.r.t ContentView。 您可以从以下网站获得详细信息: