Ios 用于Scrollview的通用自动布局&;用户界面组件

Ios 用于Scrollview的通用自动布局&;用户界面组件,ios,storyboard,autolayout,ios-universal-app,Ios,Storyboard,Autolayout,Ios Universal App,我知道自动布局,但我是一个新的通用应用程序与自动布局。我在设置约束到下图时遇到问题 你能告诉我如何设置通用应用程序的自动布局吗?我尝试使用“约束到边距”(Constraint to margin)并将前导、顶部和尾部空间固定到容器,但它不起作用。我不知道自动布局,但下面的代码也可以。。我更喜欢代码中的约束,它更易于管理、修改和理解。。我已经在所有大小的设备上测试了以下内容 注意:您不必将所有内容都放在一个文件中。。创建不同的视图,并让每个视图处理各自的约束/布局 肖像: 景观: 实际上,自动

我知道自动布局,但我是一个新的通用应用程序与自动布局。我在设置
约束到下图时遇到问题


你能告诉我如何设置通用应用程序的自动布局吗?我尝试使用“约束到边距”(Constraint to margin)并将前导、顶部和尾部空间固定到容器,但它不起作用。

我不知道自动布局,但下面的代码也可以。。我更喜欢代码中的约束,它更易于管理、修改和理解。。我已经在所有大小的设备上测试了以下内容

注意:您不必将所有内容都放在一个文件中。。创建不同的视图,并让每个视图处理各自的约束/布局

肖像:

景观:


实际上,自动布局在scrollview中的工作方式与在其他视图中不同。在滚动视图中,前导、尾随、顶部和底部约束构成了容器视图的超级视图(在本例中为滚动视图)定义的不是间距,而是“我的滚动视图应该滚动到该组件的左、右、顶部和底部的程度”

因此,要在滚动视图中使用autolayout,您必须做一些棘手的事情:

  • 设置从滚动视图到视图的前导、尾随、顶部和底部约束
  • 向scrollview添加一个子视图,您将使用该子视图作为向导视图。由于scrollview的宽度随设备的不同而变化,因此您需要一个与scrollview的宽度相关的视图,因此您可以设置与该视图相关的约束,而不是scrollview的左右边框(因为这不会定义大小,而是“您的scrollview向左或向右滚动的程度”)。此视图的高度为0,位于0,0,宽度与scrollview相同
  • 为该视图设置约束。高度=0。前导空间到容器,尾随空间到容器设置为0(这告诉您的scrollview不要滚动到导向视图的侧面)。也向scrollview添加顶部约束,因为向导视图将位于scrollview的顶部。另外,在scrollview和guide视图之间添加等宽关系(这非常重要,它将强制guide视图与scrollview具有相同的宽度,因此没有水平滚动可用)。注意,此时,您的scrollview知道它必须向左、向右和向上滚动多少,但不能向下滚动,因此您将在autolayout中看到一个错误
  • 现在构建UI,考虑到顶部、左侧和右侧的关系必须放在与向导视图的关系中,而不是与滚动视图的关系中(您想要定义间距,而不是“滚动量”)。很难将所有需要的约束放在这里,因此我在github上创建了一个示例项目:
  • 当您打开项目时,请注意,对象和scrollview之间的所有约束并不反映间距(如我所说),而是反映scrollview应该从组件滚动的距离

    希望这有帮助

    //
    //  ViewController.m
    //  test
    //
    //  Created by Brandon T on 2015-09-11.
    //  Copyright (c) 2015 Brandon T. All rights reserved.
    //
    
    #import "ViewController.h"
    
    @interface ViewController ()
    @property (nonatomic, strong) UIView *paleometerView;
    @property (nonatomic, strong) UIView *maleometerView;
    @property (nonatomic, strong) UIView *influenceView;
    @property (nonatomic, strong) UIButton *letCompanyKnowButton;
    @property (nonatomic, strong) UIButton *shareWithFriendsButton;
    @end
    
    @implementation ViewController
    
    - (instancetype)init {
        if (self = [super init]) {
            [self initControls];
        }
        return self;
    }
    
    /*- (instancetype)initWithCoder:(NSCoder *)aDecoder {
        if (self = [super initWithCoder:aDecoder]) {
            [self initControls];
        }
        return self;
    }*/
    
    /*- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
        if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
            [self initControls];
        }
        return self;
    }*/
    
    - (void)initControls {
        self.paleometerView = [[UIView alloc] init];
        self.maleometerView = [[UIView alloc] init];
        self.influenceView = [[UIView alloc] init];
        self.letCompanyKnowButton = [[UIButton alloc] init];
        self.shareWithFriendsButton = [[UIButton alloc] init];
    
        [self.paleometerView setBackgroundColor:[UIColor lightGrayColor]];
        [self.maleometerView setBackgroundColor:[UIColor grayColor]];
    
        [self.influenceView setBackgroundColor:[UIColor yellowColor]];
        [self.letCompanyKnowButton setBackgroundColor:[UIColor redColor]];
        [self.shareWithFriendsButton setBackgroundColor:[UIColor greenColor]];
    
        [self.view setBackgroundColor:[UIColor grayColor]];
    
        [self.letCompanyKnowButton setTitle:@"Let Company Know" forState:UIControlStateNormal];
        [self.shareWithFriendsButton setTitle:@"Share With Friends" forState:UIControlStateNormal];
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
    
        [self initControls];
    
        [self layoutPaleMaleView];
        [self layoutInfluenceView];
        [self layoutAllViews];
    }
    
    - (void)layoutPaleMaleView {
        UILabel *paleLabel = [[UILabel alloc] init];
        UILabel *maleLabel = [[UILabel alloc] init];
    
        [paleLabel setText:@"Paleomenter\n85%\nPale"];
        [maleLabel setText:@"Maleometer\n71%\nMale"];
    
        [paleLabel setTextAlignment:NSTextAlignmentCenter];
        [maleLabel setTextAlignment:NSTextAlignmentCenter];
    
        [paleLabel setNumberOfLines:0];
        [maleLabel setNumberOfLines:0];
    
        [paleLabel setLineBreakMode:NSLineBreakByWordWrapping];
        [maleLabel setLineBreakMode:NSLineBreakByWordWrapping];
    
        [self.paleometerView addSubview:paleLabel];
        [self.maleometerView addSubview:maleLabel];
    
        [self.paleometerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[pale]-0-|" options:0 metrics:nil views:@{@"pale":paleLabel}]];
        [self.paleometerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[pale]-0-|" options:0 metrics:nil views:@{@"pale":paleLabel}]];
    
        [self.maleometerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[male]-0-|" options:0 metrics:nil views:@{@"male":maleLabel}]];
        [self.maleometerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[male]-0-|" options:0 metrics:nil views:@{@"male":maleLabel}]];
    
        [paleLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
        [maleLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
    }
    
    - (void)layoutInfluenceView {
        UIView *left = [[UIView alloc] init];
        UIView *middle = [[UIView alloc] init];
        UIView *right = [[UIView alloc] init];
    
        [left setBackgroundColor:[UIColor blueColor]];
        [middle setBackgroundColor:[UIColor yellowColor]];
        [right setBackgroundColor:[UIColor purpleColor]];
    
        [self.influenceView addSubview:left];
        [self.influenceView addSubview:middle];
        [self.influenceView addSubview:right];
    
        //Left, right and middle all have the same width.
        [self.influenceView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[left(==middle)]-0-[middle(==right)]-0-[right(==left)]-0-|" options:0 metrics:nil views:@{@"left":left, @"middle":middle, @"right":right}]];
    
        [self.influenceView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[left]-0-|" options:0 metrics:nil views:@{@"left":left}]];
    
        [self.influenceView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[middle]-0-|" options:0 metrics:nil views:@{@"middle":middle}]];
    
        [self.influenceView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[right]-0-|" options:0 metrics:nil views:@{@"right":right}]];
    
        [left setTranslatesAutoresizingMaskIntoConstraints:NO];
        [middle setTranslatesAutoresizingMaskIntoConstraints:NO];
        [right setTranslatesAutoresizingMaskIntoConstraints:NO];
    }
    
    - (void)layoutAllViews {
        [self.view addSubview:self.paleometerView];
        [self.view addSubview:self.maleometerView];
        [self.view addSubview:self.influenceView];
        [self.view addSubview:self.letCompanyKnowButton];
        [self.view addSubview:self.shareWithFriendsButton];
    
    
        NSDictionary *views = @{@"paleometer":self.paleometerView, @"maleometer":self.maleometerView, @"influence":self.influenceView, @"letCompanyKnow":self.letCompanyKnowButton, @"share":self.shareWithFriendsButton};
    
        NSMutableArray *constraints = [[NSMutableArray alloc] init];
    
        //Constrain *Horizontally* Paleometer and Maleometer to be equal widths, 0 spacing from the left and 0 spacing from the right, with 0 spacing between them..
        [constraints addObject:@"H:|-0-[paleometer(==maleometer)]-0-[maleometer(==paleometer)]-0-|"];
    
        //Constrain *Horizontally* InfluenceView to be 0 spacing from the left and 0 spacing from the right.
        [constraints addObject:@"H:|-0-[influence]-0-|"];
    
        //Constrain *Horizontally* the "letCompanyKnowButton" with 20 spacing on the left and 20 spacing on the right..
        [constraints addObject:[NSString stringWithFormat:@"H:|-%d-[letCompanyKnow]-%d-|", 20, 20]];
    
        //Constrain *Horizontally* the "shareButton" with 20 spacing on the left and 20 spacing on the right..
        [constraints addObject:[NSString stringWithFormat:@"H:|-%d-[share]-%d-|", 20, 20]];
    
        //Constrain *Vertically* the paleometer with 0 spacing from the top and 150 in height.
        //Below it we have 0 spacing and then the influenceView which has a minimum height of 250 but is flexible so it will size to fit whenever needed.
        //Next we have another 20 spacing below the influenceView and the letCompanyKnow button which has a height of 50 and has a 20 spacing above and below it.
        //Finally the shareWithFriends button has the same height as the letCompanyKnow button.
        [constraints addObject:@"V:|-0-[paleometer(150)]-0-[influence(250@500)]-20-[letCompanyKnow(50)]-20-[share(==letCompanyKnow)]-20-|"];
    
        //Constrain *Vertically* the maleometer to be equal height with paleometer.
        [constraints addObject:@"V:[maleometer(==paleometer)]"];
    
        //Make influenceView flexible height for both shrinking and expanding vertically.
        [self.influenceView setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisVertical];
        [self.influenceView setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisVertical];
    
        //add the constraints to the view.
        for (NSString *constraint in constraints) {
            [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:constraint options:0 metrics:nil views:views]];
        }
    
        for (UIView *view in self.view.subviews) {
            [view setTranslatesAutoresizingMaskIntoConstraints:NO];
        }
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end