【问题标题】:autolayout scrollview best approach自动布局滚动视图最佳方法
【发布时间】: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


    【解决方案1】:

    许多 iOS 开发人员在使用 AutoLayoutUIScrollView 时遇到一个常见问题。这通常并不难,只需记住几件事即可。

    1. 将滚动视图添加到主 UIView 并给出与 UIView 相关的约束。
    2. ScrollView 中添加内容视图 (UIView),并针对ScrollView 提供顶部、底部、左侧和右侧约束。还添加等宽和等高约束 w.r.t main UIView
    3. 如果您需要垂直滚动,请将等高约束的优先级设置为低。
    4. 现在您可以在内容视图中添加任何内容并添加约束 w.r.t ContentView

    您可以从UIScrollView with AutoLayout获取详细信息

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-11
      相关资源
      最近更新 更多