2015-10-30 35 views
0

我没有找到一种方法来创建一个视图里面的一些视图,我用一些元素和约束创建一个xib,我创建了一个控制器scrollview-> scrollviewcontent - > {v1,v2 ,v3} bot我的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]; 

} 

回答

-1

许多iOS开发与利用AutoLayoutUIScrollView一个常见的问题。通常不是那么困难,只需记住几件事情。

  1. 对于添加滚动查看到主UIView并给予约束UIView
  2. ScrollView内部添加内容视图(UIView),并给出顶部,底部,左侧和右侧的约束条件w.r.t ScrollView。还要加上等宽和等高限制w.r.t主UIView
  3. 如果需要垂直滚动,请将等高约束的优先级设置为低。
  4. 现在您可以在内容视图中添加任何内容并添加约束条件w.r.t ContentView

您可以从UIScrollView with AutoLayout

获取详细信息