AutoLayout下CollectionView的自適應大小(三)

    這裏上接這篇文章,通過前期的準備,這裏終於開始使用AutoLayout了,有了前面的鋪墊這裏才顯得沒那麼唐突,好吧,這裏先說下故事的背景吧,對於一個View,咱們在佈局的時候會寫死它的大小,多是(320,240),可是對於不一樣的屏幕尺寸,這樣寫顯然是不合適的,對於一些簡單的佈局能夠經過autoResizing的方式自動放大,但本例view中包含高度可變的CollectionView較爲複雜,autoResizing已經不能知足咱們的需求了,因此才轉向更爲強大的AutoLayout,能夠說AutoResizing是AutoLayout的子集,AutoLayout使用起來更爲複雜,但對於簡單的問題,AutoResizing可以很簡單的解決。
函數


開始以前,咱們可能還須要回顧下這篇文章,這裏限制條件應該加在什麼地方是個問題,
佈局

加在initWithFrame能夠,但加在layoutSubviews中更合適,這樣在addSubviews或者frame變化都會引發layoutSubviews的調用,先上一段計算當前佈局高度和以前高度差值的函數spa

- (CGFloat)getRealAnimationViewHeightDt
{
    NSArray* labels = [[MFAppModel sharedObject].chatroomModelEx getFullLabels];
    
    NSInteger lineWidth = 50;
    NSUInteger rowCount = 1;
    for (int i = 1; i < labels.count; ++i) {
        NSArray* labels = [[MFAppModel sharedObject].chatroomModelEx getFullLabels];
        MFRoomLabel* currentRoomLabel = [labels objectAtIndex:i];
        NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:15]};
        CGSize currentLabelSize = [currentRoomLabel.name sizeWithAttributes:attribute];
        CGFloat cellWidth = MAX(50, currentLabelSize.width + 16);
        lineWidth = lineWidth + 15 + cellWidth;
        if (lineWidth > (self.frame.size.width - 65)) {
            rowCount++;
            lineWidth = cellWidth;
        }
    }

    return (rowCount - 2)*(24 + 10);
}

這裏默認rowCount爲2,對默認狀況計算新的高度差值,來限制佈局的大小。.net

這種代碼寫在哪裏好?雖然是和UI相關的,但這裏又夾雜着邏輯的數據,目前IOS開發中ViewController的使用仍是比較迷惑,這裏究竟是放View仍是也能夠放些和UI相關的數據呢?這種破壞MVC模式的代碼到底寫在Model層仍是View層?之後再討論吧,這裏先說AutoLayout的問題。code

    有了這個根據數據和當前屏幕尺寸計算出來的collectionView的dt值,就能夠方便的加些約束條件了,這裏首先你能夠參考下這篇文章,這個例子很簡單,都是受到加約束條件的最簡單的用法,
blog

下面上咱們的代碼ip

[self addConstraint:[NSLayoutConstraint
                         constraintWithItem:_collectionView
                         attribute:NSLayoutAttributeWidth
                         relatedBy:NSLayoutRelationEqual
                         toItem:nil
                         attribute:NSLayoutAttributeWidth
                         multiplier:0
                         constant:self.frame.size.width - 65]];

    [self addConstraint:[NSLayoutConstraint
                         constraintWithItem:_collectionView
                         attribute:NSLayoutAttributeHeight
                         relatedBy:NSLayoutRelationEqual
                         toItem:nil
                         attribute:NSLayoutAttributeHeight
                         multiplier:0
                         constant:_collectionView.frame.size.height + [self getRealAnimationViewHeightDt]]];
    
    [self addConstraint:[NSLayoutConstraint
                         constraintWithItem:_mainView
                         attribute:NSLayoutAttributeWidth
                         relatedBy:NSLayoutRelationEqual
                         toItem:self
                         attribute:NSLayoutAttributeWidth
                         multiplier:1
                         constant:0]];
    
    [self addConstraint:[NSLayoutConstraint
                         constraintWithItem:_mainView
                         attribute:NSLayoutAttributeHeight
                         relatedBy:NSLayoutRelationEqual
                         toItem:nil
                         attribute:NSLayoutAttributeHeight
                         multiplier:0
                         constant:_mainView.frame.size.height + [self getRealAnimationViewHeightDt]]];
    
    [self addConstraint:[NSLayoutConstraint
                         constraintWithItem:_animationView
                         attribute:NSLayoutAttributeWidth
                         relatedBy:NSLayoutRelationEqual
                         toItem:self
                         attribute:NSLayoutAttributeWidth
                         multiplier:1
                         constant:0]];
    
    [self addConstraint:[NSLayoutConstraint
                         constraintWithItem:_animationView
                         attribute:NSLayoutAttributeHeight
                         relatedBy:NSLayoutRelationEqual
                         toItem:nil
                         attribute:NSLayoutAttributeHeight
                         multiplier:0
                         constant:_animationView.frame.size.height + [self getRealAnimationViewHeightDt]]];
    
    [self addConstraint:[NSLayoutConstraint
                         constraintWithItem:_buttonView
                         attribute:NSLayoutAttributeWidth
                         relatedBy:NSLayoutRelationEqual
                         toItem:self
                         attribute:NSLayoutAttributeWidth
                         multiplier:1
                         constant:0]];
    
    [self addConstraint:[NSLayoutConstraint
                         constraintWithItem:_cancelButton
                         attribute:NSLayoutAttributeWidth
                         relatedBy:NSLayoutRelationEqual
                         toItem:self
                         attribute:NSLayoutAttributeWidth
                         multiplier:0.5
                         constant:0]];
    
    [self addConstraint:[NSLayoutConstraint
                         constraintWithItem:_cancelButton
                         attribute:NSLayoutAttributeLeft
                         relatedBy:NSLayoutRelationEqual
                         toItem:self
                         attribute:NSLayoutAttributeLeft
                         multiplier:1
                         constant:0]];
    
    [self addConstraint:[NSLayoutConstraint
                         constraintWithItem:_okButton
                         attribute:NSLayoutAttributeWidth
                         relatedBy:NSLayoutRelationEqual
                         toItem:self
                         attribute:NSLayoutAttributeWidth
                         multiplier:0.5
                         constant:0]];
    
    [self addConstraint:[NSLayoutConstraint
                         constraintWithItem:_okButton
                         attribute:NSLayoutAttributeRight
                         relatedBy:NSLayoutRelationEqual
                         toItem:self
                         attribute:NSLayoutAttributeRight
                         multiplier:1
                         constant:0]];
    
    [self addConstraint:[NSLayoutConstraint
                         constraintWithItem:_buttonView
                         attribute:NSLayoutAttributeTop
                         relatedBy:NSLayoutRelationEqual
                         toItem:_lineView
                         attribute:NSLayoutAttributeBottom
                         multiplier:1
                         constant:0]];

    [self addConstraint:[NSLayoutConstraint
                         constraintWithItem:_sortAndPositionLineView
                         attribute:NSLayoutAttributeWidth
                         relatedBy:NSLayoutRelationEqual
                         toItem:self
                         attribute:NSLayoutAttributeWidth
                         multiplier:1
                         constant:0]];
    
    [self addConstraint:[NSLayoutConstraint
                         constraintWithItem:_positionAndTagLineView
                         attribute:NSLayoutAttributeWidth
                         relatedBy:NSLayoutRelationEqual
                         toItem:self
                         attribute:NSLayoutAttributeWidth
                         multiplier:1
                         constant:0]];
    
    [self addConstraint:[NSLayoutConstraint
                         constraintWithItem:_lineView
                         attribute:NSLayoutAttributeWidth
                         relatedBy:NSLayoutRelationEqual
                         toItem:self
                         attribute:NSLayoutAttributeWidth
                         multiplier:1
                         constant:0]];
    
    [self addConstraint:[NSLayoutConstraint
                         constraintWithItem:_lineView
                         attribute:NSLayoutAttributeTop
                         relatedBy:NSLayoutRelationEqual
                         toItem:_mainView
                         attribute:NSLayoutAttributeBottom
                         multiplier:1
                         constant:0]];
    
    [self addConstraint:[NSLayoutConstraint
                         constraintWithItem:_buttonLineView
                         attribute:NSLayoutAttributeCenterX
                         relatedBy:NSLayoutRelationEqual
                         toItem:self
                         attribute:NSLayoutAttributeCenterX
                         multiplier:1
                         constant:0]];
    
    [self addConstraint:[NSLayoutConstraint
                         constraintWithItem:_lineView
                         attribute:NSLayoutAttributeWidth
                         relatedBy:NSLayoutRelationEqual
                         toItem:self
                         attribute:NSLayoutAttributeWidth
                         multiplier:1
                         constant:0]];

想一想這些約束條件要是加在IB中還能看嗎?這也就是說AutoLayout複雜的地方,對於屏幕尺寸變化的狀況,裏面的全部view都須要根據這個最外層的屏幕尺寸而發生變化,再加上View中又有高度不可控的collectionView,真是約束的不亦樂乎,但回頭想一想,這也不失爲一種解決問題的好辦法,有沒有更簡單的解決問題的方法還有待挖掘,就目前對AutoLayout的理解,只能是這樣的實現了開發

這個是不一樣屏幕尺寸的效果,能夠看到,當屏幕分辨率小的時候,collectionView的高度變化,但能夠從新佈局適應不一樣屏幕。get

相關文章
相關標籤/搜索