天天看点

autolayout - sizeClass - Masonry - 3

学习完autolayout和Masonry的基本知识点后,马上编写几个小例子来玩玩:

这个例子取自Protocol - 3 用委托来实现页面间的传值,可以参见那篇文章,例子大概就是在视图上放置一个textView,然后改变textView的内容,我试着用Masonry来达到旋转屏幕(横屏或者竖屏的自适配)

刚开始的代码实现:

第一种:值要适当取得好:

#import "Masonry.h"
#import "LBProtoFirstViewController.h"

@interface LBProtoFirstViewController ()

@property (nonatomic, strong) UITextView* textView;

@end

@implementation LBProtoFirstViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIView* superView = self.view;
    
    self.textView = [UITextView new];
    self.textView.textColor = [UIColor redColor];
    self.textView.font = [UIFont systemFontOfSize:22];
    self.textView.backgroundColor = [UIColor lightGrayColor];
    self.textView.translatesAutoresizingMaskIntoConstraints = NO;
    self.textView.text = @"Even with such a simple example the code needed is quite verbose and quickly ";
    [superView addSubview:self.textView];

    UIEdgeInsets padding = UIEdgeInsetsMake(100, 50, 150, 50);

    [self.textView mas_makeConstraints:^(MASConstraintMaker* make){
        
        make.edges.equalTo(superView).with.insets(padding);
    
    }];
    
}
           

第一种方法如果设置的值不适当的话,当切换成横屏的时候textView会直接消失了,说白了就是写死了。

第二种:在旋转的扩展方法中添加处理(这个处理就是一个update更新)

#import "Masonry.h"
#import "LBProtoFirstViewController.h"

@interface LBProtoFirstViewController ()

@property (nonatomic, strong) UITextView* textView;

@end

@implementation LBProtoFirstViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIView* superView = self.view;
    
    self.textView = [UITextView new];
    self.textView.textColor = [UIColor redColor];
    self.textView.font = [UIFont systemFontOfSize:22];
    self.textView.backgroundColor = [UIColor lightGrayColor];
    self.textView.translatesAutoresizingMaskIntoConstraints = NO;
    self.textView.text = @"Even with such a simple example the code needed is quite verbose and quickly ";
    [superView addSubview:self.textView];
    
    UIEdgeInsets padding = UIEdgeInsetsMake(100, 50, 280, 50);

    [self.textView mas_makeConstraints:^(MASConstraintMaker* make){
        
        make.edges.equalTo(superView).with.insets(padding);
    
    }];
    
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    
    BOOL isLandScape = NO;
    UIDeviceOrientation oritation  = [UIDevice currentDevice].orientation;
    isLandScape = ((oritation == UIDeviceOrientationLandscapeLeft) || (oritation == UIDeviceOrientationLandscapeRight));
    
    if (isLandScape)
    {
        UIEdgeInsets padding = UIEdgeInsetsMake(20, 50, 50, 50);
        
        [self.textView mas_updateConstraints:^(MASConstraintMaker* make){
            
            make.edges.equalTo(self.view).with.insets(padding);
            
        }];
        
    }
    else
    {
        UIEdgeInsets padding = UIEdgeInsetsMake(100, 50, 280, 50);
        
        [self.textView mas_updateConstraints:^(MASConstraintMaker* make){
            
            make.edges.equalTo(self.view).with.insets(padding);
            
        }];
    }
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{

}

@end
           

注意如果把处理添加到did方法里会有闪烁的不好的视觉体验。

同理当使用center属性的时候:

#import "Masonry.h"
#import "LBProtoFirstViewController.h"

@interface LBProtoFirstViewController ()

@property (nonatomic, strong) UITextView* textView;

@end

@implementation LBProtoFirstViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIView* superView = self.view;
    
    self.textView = [UITextView new];
    self.textView.textColor = [UIColor redColor];
    self.textView.font = [UIFont systemFontOfSize:22];
    self.textView.backgroundColor = [UIColor lightGrayColor];
    self.textView.translatesAutoresizingMaskIntoConstraints = NO;
    self.textView.text = @"Even with such a simple example the code needed is quite verbose and quickly !";
    [superView addSubview:self.textView];
    
    [self.textView mas_makeConstraints:^(MASConstraintMaker* make){
        
        make.center.mas_equalTo(superView);
        
        make.size.mas_equalTo(CGSizeMake([[UIScreen mainScreen] bounds].size.width - 50, [[UIScreen mainScreen] bounds].size.height - 150));
    
    }];
    
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{

    BOOL isLandScape = NO;
    UIDeviceOrientation oritation  = [UIDevice currentDevice].orientation;
    isLandScape = ((oritation == UIDeviceOrientationLandscapeLeft) || (oritation == UIDeviceOrientationLandscapeRight));
    
    if (isLandScape)
    {
        [self.textView mas_updateConstraints:^(MASConstraintMaker* make){
            
            
            make.center.mas_equalTo(self.view);
            
            make.size.mas_equalTo(CGSizeMake([[UIScreen mainScreen] bounds].size.width - 50, [[UIScreen mainScreen] bounds].size.height - 80));
            
        }];
    
    }
    else
    {
        [self.textView mas_updateConstraints:^(MASConstraintMaker* make){
            
            
            make.center.mas_equalTo(self.view);
            
            make.size.mas_equalTo(CGSizeMake([[UIScreen mainScreen] bounds].size.width - 50, [[UIScreen mainScreen] bounds].size.height - 150));
            
        }];
    }
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{

    
}

@end
           

这个是小弟目前想到的最直接的处理横竖屏的方法,如果以后有更好的方法会直接在这里更新!

8月10日晚更新:

然而,如果用autolayout来实现要用到判断方向是十分失败的,因为autolayout本身就应该符合完全自适应 - 包括对屏幕方向的敏感度!

所以上面的例子应该使用offset的属性,从而避免判断方向!