iOS 自動布局擴充應用:代碼中動態調整布局常量

來源:互聯網
上載者:User

iOS 自動布局擴充應用:代碼中動態調整布局常量
一.設計需求

iOS Storyboard 自動布局技術,是iOS 6才出來的新技術,相當於多螢幕分辯率下自適應的技術。

但是一些複雜情況還是難處理。

比如有一個介面需求,進度條上顯示標籤,這個需求不難,難的是顯要在顯示表格框內,在各種機型顯示正常。

最初設定是垂直置中向上偏15個像素

這是iPhone 4S 顯示效果,下面與滑塊還有錯位

但是在iPhone 6下顯示,下面有錯位,但是上面留空太多

但如果把位移量設為21.則出現另一種情況。

大螢幕的手機顯示完美。

但是iPhone 4S下就錯位了

因此我們如何解決這問題呢,如果能找到一種方法,在代碼中支援調整這個一些常量,比如在iPhone 4S下我把它設為15,在Iphone6下高為21,這個問題就解決了。

二.代碼中調整常量

事實上,iOS還提供這樣方法來調整,一種方法是手工在代碼中建立這個約束常量,然後不同機型賦值不一樣。

另一種方法是在控制項找到它的約束變數,用代碼調整值即可,後者更為簡單,因為我可以在Storyboard 針對大部分機型設定好變數,然後在小螢幕機型,微調一下即可。

兩者測試均可實現,我現在實現後一種:

//尋找對應的約束常量,注意分清是firstItem,還是secondItem.這個在Storybord能看說明,比哪區中對齊時,被對齊的控制項是secondItem.+(NSLayoutConstraint *) findFirstConstraint:(UIView *)parentView firstItem:(id)firstItem  attribute:(NSLayoutAttribute)attribute{    for (NSLayoutConstraint *constraint in parentView.constraints) {       constraint.priority, constraint.secondItem);        if((constraint.firstItem == firstItem) && (constraint.firstAttribute == attribute)){            NSLog(@"find! %@",firstItem);            return constraint;        }    }    return nil;}+(NSLayoutConstraint *) findSecondConstraint:(UIView *)parentView secondItem:(id)secondItem  attribute:(NSLayoutAttribute)attribute{    for (NSLayoutConstraint *constraint in parentView.constraints) {        if((constraint.secondItem == secondItem) && (constraint.secondAttribute == attribute)){            NSLog(@"find! %@",secondItem);            return constraint;        }    }    return nil;}

在建立方法中,判斷一下是否是iPhone4S 小屏,如果將約束常量和字型均縮小,以便能加入。這裡將對齊位移量由21變成15,字型大小也縮小了2號

-(void)relayoutLabel{       //模擬器下總是 iPhone6 /iPhone5--> 320x568    //           iPhone4S --> 320x480    //    CGSize result = [[UIScreen mainScreen] bounds].size;    if(result.height > 500)    {        return ;    }    //iPhone 4S 以下版本    NSLayoutConstraint *constraint;    //找到約束常量    constraint = [Utils findSecondConstraint:self.banner01 secondItem:self.percent attribute:NSLayoutAttributeCenterY];    //    //    //垂直置中    //    constraint = [NSLayoutConstraint    //                  constraintWithItem:self.banner01    //                  attribute:NSLayoutAttributeCenterY    //                  relatedBy:NSLayoutRelationEqual    //                  toItem:self.percent    //                  attribute:NSLayoutAttributeCenterY    //                  multiplier:1.0f    //                  constant:15.0f];    // [self.banner01 addConstraint:constraint];    constraint.constant = 15; //由21--》15    NSLog(@"constraint %f,%d",constraint.constant,constraint.firstAttribute);    UIFont * font =  [UIFont fontWithName:@"Helvetica" size:12]; //字型由14--》變成 12    [self.percent setFont:font];}
三.最終效果

最終實現效果:(iPhone 4S 還是有點錯開,但是已經最大努力了,如果想完全錯開,可以把字型大小變成10)

iPhone4S

iPhone 6效果

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.