• 如何在scrollView中使用Autolayout


    prat1 storyboard情况下进行约束布局  

    scrollView比较特殊,因为它有个contentSize的属性。那么在遇到scrollView时,怎么使用Autolayout呢。其实关键点就一点:

    ScrollView的contentSize的大小是由其subview的constraints来决定的。

     转:http://blog.cnbluebox.com/blog/2015/02/02/autolayout2/

    向竖滚动步骤:

    1.设置subview四个方向的边与scrollview四个方向的边相贴的约束。

    2.设置subview和scrollview等宽和等高的约束,等高约束优先级要比subview后面具体高度的约束低。

    3.设置subview具体高度的约束, 当改变该约束时与设置contentSize.heigth作用等效。

    part2手写代码情况下进行约束布局

    条件通过自定义scrollview,覆盖addSubview:和setContentSize:方法。添加一个subview,改变改subview的height值为目标的contentSize.height。

    自定义scrollView的代码如下:

    #import <UIKit/UIKit.h>
    
    @interface MScrollView : UIScrollView
    
    @property(nonatomic,strong,readonly)UIView * mContentView;
    
    @end

    .

    #import "MScrollView.h"
    
    @interface MScrollView ()
    @property(nonatomic,strong) UIView * mContentView;
    @end
    
    @implementation MScrollView
    
    
    -(instancetype)initWithFrame:(CGRect)frame{
        if (self=[super initWithFrame:frame]) {
            _mContentView = [[UIView alloc]initWithFrame:frame];
            [self addSubview:_mContentView];
        }
        return self;
    }
    
    -(void)addSubview:(UIView *)view{
        [super addSubview:view];
        if (view!=_mContentView) {
            [_mContentView addSubview:view];
        }else{
            [super addSubview:_mContentView];
        }
    }
    
    -(void)setContentSize:(CGSize)contentSize{
        _mContentView.frame = CGRectMake(self.frame.origin.x
                                         , self.frame.origin.y, contentSize.width, contentSize.height);
        [super setContentSize:contentSize];
    }
    
    @end

     .

    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.scrollView = [[MScrollView alloc]initWithFrame:self.view.frame];
        self.scrollView.backgroundColor = [UIColor redColor];
        self.scrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 1000);
        [self.view addSubview:self.scrollView];
        
        UIView * blueView  =[[UIView alloc]init];
        blueView.backgroundColor =[ UIColor blueColor];
        blueView.tag = 1008;
        [self.scrollView.mContentView addSubview:blueView];
        
    }
    
    -(void)updateViewConstraints{
        [super updateViewConstraints];
        UIView *blueView = [self.scrollView.mContentView viewWithTag:1008];
        //
        CGFloat width = [UIScreen mainScreen].bounds.size.width;
        NSDictionary *metrics = @{@"width":@(width),@"height":@(200)};
        blueView.translatesAutoresizingMaskIntoConstraints = NO;
        [blueView.superview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[blueView(==width)]" options:0 metrics:metrics views:@{@"blueView":blueView}]];
        [blueView.superview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[blueView(==height)]" options:0 metrics:metrics views:@{@"blueView":blueView}]];
        [blueView.superview addConstraint:[NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:blueView.superview attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
    
    }
  • 相关阅读:
    在 SQL2005 使用行转列或列转行
    JOIN 和 WHERE?简单的问题也有学问。
    完整的获取表结构语句
    经典背景音乐收集
    interface 是什么类型?
    WMI使用集锦
    存储过程+游标,第一回开发使用
    Sql Server 基本语句汇总
    PowerDesigner 设置各项变量参数的路径
    测试
  • 原文地址:https://www.cnblogs.com/huen/p/4543878.html
Copyright © 2020-2023  润新知