我没有找到创建内部某个视图的滚动视图的方法,我创建了一个带有一些元素和一些约束的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];
}
答案 0 :(得分:-1)
许多iOS开发人员在使用AutoLayout
UIScrollView
时遇到一个常见问题。这通常不是那么困难,只需记住几件事。
UIView
并提供与UIView
相关的约束。UIView
内添加内容视图(ScrollView
),并给出顶部,底部,左侧和右侧约束w.r.t ScrollView
。还要添加相等宽度和相等高度约束w.r.t main UIView
。ContentView
。