Autoresizing的问题

作者: Z了个L | 来源:发表于2016-04-15 21:38 被阅读80次
    • 一般情况下,以下这些view的autoresizingMask默认就是18(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)

      • 1.从xib里面创建出来的默认控件(Xcode自动创建出来的那个view)
      • 2.控制器的view
    • 如果不希望控件拥有autoresizingMask的自动伸缩功能,应该设置为none

    • ViewController
    
    // ViewController.h
    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController
    
    
    @end
    
    // ViewController.m
    #import "ViewController.h"
    
    /*
     一般情况下,以下这些view的autoresizingMask默认就是18(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)
     1.从xib里面创建出来的默认控件(Xcode自动创建出来的那个view)
     2.控制器的view
    
     如果不希望控件拥有autoresizingMask的自动伸缩功能,应该设置为none
     blueView.autoresizingMask = UIViewAutoresizingNone;
     */
    
    @interface ViewController ()
    /** redView */
    @property (nonatomic, weak) UIView *redView;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        UIView *redView = [[UIView alloc] init];
        redView.backgroundColor = [UIColor redColor];
        redView.frame = CGRectMake(20, 20, 300, 200);
        [self.view addSubview:redView];
        self.redView = redView;
    
        UIView *blueView = [[NSBundle mainBundle] loadNibNamed:@"BlueView" owner:nil options:nil].firstObject;
        // 去掉默认的自动拉伸宽高功能
    //    blueView.autoresizingMask = UIViewAutoresizingNone;
        [redView addSubview:blueView];
    
        UIView *yellowView = [[UIView alloc] init];
        yellowView.backgroundColor = [UIColor yellowColor];
        yellowView.frame = CGRectMake(0, 0, 200, 150);
        [redView addSubview:yellowView];
    
        // 18 == UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight
        // 0 == UIViewAutoresizingNone
    }
    
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
        CGRect frame = self.redView.frame;
        frame.size.width -= 10;
        frame.size.height += 10;
        self.redView.frame = frame;
    }
    
    @end
    
    
    
    • 显示效果图片:

    相关文章

      网友评论

        本文标题:Autoresizing的问题

        本文链接:https://www.haomeiwen.com/subject/bhvnlttx.html