自动布局+的UIScrollView和单子视图 [英] Auto Layout + UIScrollView and single subview

查看:172
本文介绍了自动布局+的UIScrollView和单子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是自动布局和UIScrollView的玩弄。我缺少的是在这里吗?这将显示在屏幕底部的蓝色滚动视图,但我看不到的内容查看。视图调试器无关,它要么说。有任何想法吗?

  //
// ViewController.m
//飞度
//
//创建者亚当·达昂在2015年3月13日。
//版权所有(C)2015年亚当大汉。版权所有。
//#进口ViewController.h@interface的ViewController()
{
    *的UIScrollView滚动视图;
    UIView的*内容查看;
}
@结束@implementation的ViewController - (无效)viewDidLoad中
{
    [超级viewDidLoad中];    [个体经营createViews]
    [个体经营constrainScrollView]
    [个体经营constrainContentView]
} - (无效)createViews
{
指令#pragma初始化滚动视图
    滚动视图= [[UIScrollView的页头] initWithFrame:方法CGRectZero];
    scrollView.translatesAutoresizingMaskIntoConstraints = NO;
    scrollView.backgroundColor =的UIColor blueColor]
    scrollView.showsHorizo​​ntalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:滚动视图]。初始化的#pragma为滚动型内容查看
    内容查看= [[UIView的页头] initWithFrame:方法CGRectZero];
    contentView.backgroundColor =的UIColor redColor]
    contentView.translatesAutoresizingMaskIntoConstraints = NO;
    [滚动视图addSubview:内容查看]。
} - (无效)constrainScrollView
{滚动视图的#pragma垂直约束
    NSArray的CNS * = [NSLayoutConstraint constraintsWithVisualFormat:@V:[滚动视图(40)|
                                                       选项​​:0
                                                       指标:无
                                                                观点:NSDictionaryOfVariableBindings(滚动视图)];
    [self.view addConstraints:CNS]滚动视图的#pragma水平的限制
    CNS = [NSLayoutConstraint constraintsWithVisualFormat:@H:| [滚动视图] |
                                              选项​​:0
                                              指标:无
                                                观点:NSDictionaryOfVariableBindings(滚动视图)];
    [self.view addConstraints:CNS]
} - (无效)constrainContentView
{的#pragma内容查看垂直约束
    NSArray的CNS * = [NSLayoutConstraint constraintsWithVisualFormat:@V:| [内容查看] |
                                              选项​​:0
                                              指标:无
                                                观点:NSDictionaryOfVariableBindings(滚动视图,内容查看)];
    [滚动视图addConstraints:CNS]的#pragma内容查看水平限制
    CNS = [NSLayoutConstraint constraintsWithVisualFormat:@H:| [内容查看] |
                                              选项​​:0
                                              指标:无
                                                观点:NSDictionaryOfVariableBindings(滚动视图,内容查看)];
    [滚动视图addConstraints:CNS]
}@结束


解决方案

ScrollViews使用自动布局时更为复杂一点点。该框架是由与其他意见的约束(你会在你 constainScrollView 添加的那些方法)。

定义

contentSize 取决于从里面滚动视图的意见的约束定义。对于你的情况,该contentSize是(0,0),因为内部视图没有宽度或高度约束。

我想你会喜欢这个内部景观,填补了滚动视图。那么你应该限制看起来是这样的:

   - (无效)constrainContentView
{
    [self.view layoutIfNeeded] //这将导致滚动视图的宽度和高度来计算基于您在previous方法发送的限制
    //你可以通过这里添加一个断点检查,如果有滚动视图框,你会期望的#pragma内容查看垂直约束
    NSArray的CNS * = [NSLayoutConstraint constraintsWithVisualFormat:@V:| - [内容查看(高度)] - |
                                              选项​​:0
                                              指标:@ {身高:@(CGRectGetHeight(scrollView.frame))}
                                                观点:NSDictionaryOfVariableBindings(滚动视图,内容查看)];
    [滚动视图addConstraints:CNS]的#pragma内容查看水平限制
    CNS = [NSLayoutConstraint constraintsWithVisualFormat:@H:| - [内容查看(宽)] - |
                                              选项​​:0
                                              指标:@ {宽度:@(CGRectGetWidth(self.view.frame))}
                                                观点:NSDictionaryOfVariableBindings(滚动视图,内容查看)];
    [滚动视图addConstraints:CNS]
}

您的宽度设置的实际值高度取决于你的需要。

重要的是,里面的滚动视图需要有以这样的方式设置的限制,该 contentSize 可以明确进行计算。这意味着,如果认为不具有内在的尺寸(如的UIButton 的UILabel ),你需要添加宽度/身高的限制明确。

由于设置宽度和高度明确,对方向改变,你会需要更新约束,这样的意见,里面有合适的尺寸。

希望这会解决您的问题。

Just playing around with Auto Layout and UIScrollView. What am I missing here? This displays a blue scrollView at the bottom of the screen but I cannot see the contentView. The view debugger has nothing to say for it either. Any ideas?

//
//  ViewController.m
//  Fit
//
//  Created by Adam Dahan on 2015-03-13.
//  Copyright (c) 2015 Adam Dahan. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
{
    UIScrollView *scrollView;
    UIView *contentView;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self createViews];
    [self constrainScrollView];
    [self constrainContentView];
}

- (void)createViews
{
#pragma Initialize a scrollView
    scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
    scrollView.translatesAutoresizingMaskIntoConstraints = NO;
    scrollView.backgroundColor = [UIColor blueColor];
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:scrollView];

#pragma Initialize a contentView for the scrollView
    contentView = [[UIView alloc] initWithFrame:CGRectZero];
    contentView.backgroundColor = [UIColor redColor];
    contentView.translatesAutoresizingMaskIntoConstraints = NO;
    [scrollView addSubview:contentView];
}

- (void)constrainScrollView
{

#pragma scrollView vertical constraints
    NSArray *cns = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[scrollView(40)]|"
                                                       options:0
                                                       metrics:nil
                                                                views:NSDictionaryOfVariableBindings(scrollView)];
    [self.view addConstraints:cns];

#pragma scrollView horizontal constraints
    cns = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|"
                                              options:0
                                              metrics:nil
                                                views:NSDictionaryOfVariableBindings(scrollView)];
    [self.view addConstraints:cns];
}

- (void)constrainContentView
{

#pragma contentView vertical constraints
    NSArray *cns = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[contentView]|"
                                              options:0
                                              metrics:nil
                                                views:NSDictionaryOfVariableBindings(scrollView, contentView)];
    [scrollView addConstraints:cns];

#pragma contentView horizontal constraints
    cns = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[contentView]|"
                                              options:0
                                              metrics:nil
                                                views:NSDictionaryOfVariableBindings(scrollView, contentView)];
    [scrollView addConstraints:cns];
}

@end

解决方案

ScrollViews are a little bit more complicated when using auto layout. The frame is defined from the constraints with the other views (for you would be the ones you add in constainScrollView method).

But the contentSize is defined depending on the constraints from the views inside the scrollView. For your case, the contentSize is (0,0) because the inner view has no width or height constraint.

I suppose you would like this inner view to fill the scrollview. Then your constraints should look something like this:

- (void)constrainContentView
{
    [self.view layoutIfNeeded]; // This causes the scrollview width and height to be calculated based on the constraints you sent in the previous method
    // You could check by adding a breakpoint here, if the scrollview has the frame you would expect

#pragma contentView vertical constraints
    NSArray *cns = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[contentView(height)]-|"
                                              options:0
                                              metrics:@{"height":@(CGRectGetHeight(scrollView.frame))}
                                                views:NSDictionaryOfVariableBindings(scrollView, contentView)];
    [scrollView addConstraints:cns];

#pragma contentView horizontal constraints
    cns = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[contentView(width)]-|"
                                              options:0
                                              metrics:@{"width":@(CGRectGetWidth(self.view.frame))}
                                                views:NSDictionaryOfVariableBindings(scrollView, contentView)];
    [scrollView addConstraints:cns];
}

The actual value you set for the width and height depends on your needs.

The important thing is that the views inside the scrollView need to have the constraints in such a way setup that the contentSize can be calculated without ambiguity. This means if the view does not have an intrinsic size (like a UIButton or UILabel) you need to add the width/height constraints explicitly.

Because of setting the width and height explicitly, on orientation change you would need to update the constraints so that the views inside have the right dimensions.

Hope this fixes your problem.

这篇关于自动布局+的UIScrollView和单子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆