您的位置:首页 > 其它

autolayout - sizeClass - 4

2015-06-30 22:25 183 查看
经过上面几篇大牛的博客我想大概得肯定知道什么时候该用autoLayout,什么时候该用frame code的布局了吧

其中一点就是iPhone和iPad的共适配的时候该用AL布局,而之所以要用无非就是因为iPad的屏幕横竖转屏造成的界面元素的重新布局:

这里有一篇非常优秀的文章:http://blog.csdn.net/liangliang103377/article/details/40082271 可作为参考。

而其中提到的一个方法,尤其重要:

/* 
 This method is called when the view controller's trait collection is changed by its parent.
 
 If you override this method, you should either call super to propagate the change to children or manually forward the change to children.
 */
- (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator NS_***AILABLE_IOS(8_0);


下面原搬大牛的例子:

@interface AutoLayoutPracController ()
{
    UIView* greenView;
    UIView* yellowView;
    UIView* blueView;
}

@end


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self.view setBackgroundColor:[UIColor lightGrayColor]];
    
    self.view.translatesAutoresizingMaskIntoConstraints = NO;

    greenView = [self alView];
    greenView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:greenView];
    
    yellowView = [self alView];
    yellowView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:yellowView];
    
    blueView = [self alView];
    blueView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:blueView];
    
}

//量产UIView
- (UIView*)alView
{
    UIView* newView = [UIView new];
    newView.translatesAutoresizingMaskIntoConstraints = NO;
    
    return newView;
}

//真正上这片博客与别的博客的不一样!   
//竖屏的约束 , 要注意的是,当作为参数传入局部变量的时候,VFL语句的名字也要和参数名字相同 : greenView_   @"[greenView_]"
- (NSMutableArray*)portraitConstraintsWithGreenView:(UIView*)greenView_ yellowView:(UIView*)yellowView_ blueView:(UIView*)blueView_
{
    NSMutableArray* constraintArray = [NSMutableArray array];
    
    [constraintArray addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[greenView_]-20-[yellowView_(==greenView_)]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(greenView_, yellowView_)]];
    
    [constraintArray addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[greenView_]-20-[blueView_(==greenView_)]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(greenView_, blueView_)]];
    
    [constraintArray addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[yellowView_]-20-[blueView_(==yellowView_)]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(yellowView_, blueView_)]];
    
    [constraintArray addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[blueView_]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(blueView_)]];
    
    return constraintArray;
}

//横屏的操作
- (NSMutableArray*)landscapeConstraintsWithGreenView:(UIView*)greenView_ yellowView:(UIView*)yellowView_ blueView:(UIView*)blueView_
{
    NSMutableArray* constraintArray = [NSMutableArray array];
    
    [constraintArray addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[greenView_]-20-[yellowView_(==greenView_)]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(greenView_, yellowView_)]];
    
    [constraintArray addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[blueView_]-20-[greenView_(==blueView_)]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(greenView_, blueView_)]];
    
    [constraintArray addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[blueView_]-20-[yellowView_(==blueView_)]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(blueView_, yellowView_)]];
    
    [constraintArray addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[blueView_]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(blueView_)]];
    
    return constraintArray;
}


/** 本篇文章最值钱就是这里了,横竖屏幕的转化 **/
- (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    [super willTransitionToTraitCollection:newCollection withTransitionCoordinator:coordinator];
    
    [coordinator animateAlongsideTransition:^(id <UIViewControllerTransitionCoordinatorContext> context){
        
        if (newCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact)
        {
            NSLog(@"%s --- %d", __FUNCTION__, __LINE__);
            [self.view removeConstraints:self.view.constraints];
            [self.view addConstraints:[self landscapeConstraintsWithGreenView:greenView
                                                                   yellowView:yellowView
                                                                     blueView:blueView]];
    
        }
        else
        {
            NSLog(@"%s --- %d", __FUNCTION__, __LINE__);
            [self.view removeConstraints:self.view.constraints];
            [self.view addConstraints:[self portraitConstraintsWithGreenView:greenView
                                                                  yellowView:yellowView
                                                                    blueView:blueView]];
        }
        
             } completion:nil];
    
}

@end


当你的设备转换屏幕的时候,iOS就会自动添加约束并且是伴随着动画效果的哦。

注:转自:http://blog.csdn.net/liangliang103377/article/details/40082271

待续...
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: