成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

iOS6+AutoLayout使用教程-創(chuàng)新互聯(lián)

為了適配iPhone6 Plus,必須學(xué)會AutoLayout了,研究了兩天,記得一下心得iOS6+AutoLayout使用教程。

Autolayout類似于安卓里的layout,或者說類似于web開發(fā)中的html css,簡單來說就是用相對布局來代替絕對成局(這是我的理解)。

網(wǎng)站制作、成都網(wǎng)站制作服務(wù)團(tuán)隊是一支充滿著熱情的團(tuán)隊,執(zhí)著、敏銳、追求更好,是創(chuàng)新互聯(lián)的標(biāo)準(zhǔn)與要求,同時竭誠為客戶提供服務(wù)是我們的理念。成都創(chuàng)新互聯(lián)把每個網(wǎng)站當(dāng)做一個產(chǎn)品來開發(fā),精雕細(xì)琢,追求一名工匠心中的細(xì)致,我們更用心!

使用Autolayout有兩種方式,1.用xib配置,2.用代碼寫。

無論用xib還是代碼,需要遵循的一個原則是,一個視圖你必須包含以下兩個限制:1.position,設(shè)定x,y坐標(biāo)、上下左右間隙、居中等這些條件都能確定位置,但這幾個是相互沖突的,只能用其一;2.size, 必須設(shè)定尺寸,可以設(shè)定固定值的寬高,也可以通過設(shè)定寬高比例來等比縮放。

1.用xib配置

這種方式比較直觀,打開xib進(jìn)行配置即可,具體的使用方法網(wǎng)路上有很多介紹,這里就不細(xì)說了

2.用代碼寫

用xib配置應(yīng)該能完成大部分需要,不過還是會有要用代碼寫的時候,下面是我寫的一個例子,包含了option, metric的用法。

這個例子的效果是:xib里放置了兩個水平居中的butt1,butt3,然后用代碼創(chuàng)建了butt2,butt2與butt3水平居中對齊,最后創(chuàng)建了view1,view1只與根視圖進(jìn)行布局,與不受其他視圖影響

  //butt2

  {

    UIButton *butt = [UIButton buttonWithType:UIButtonTypeCustom];

    butt.backgroundColor = [UIColor redColor];

    [butt setTitle:@"2" forState:UIControlStateNormal];

    butt.translatesAutoresizingMaskIntoConstraints = NO;

    butt.frame = CGRectMake(0, 0, 100, 100);  //這個frame其實是不需要的

    [self.view addSubview:butt];

    butt2 = butt;

    //寬:高 = 1:1

    [butt2 addConstraint:[NSLayoutConstraint constraintWithItem:butt2 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:butt2 attribute:NSLayoutAttributeHeight multiplier:1 constant:0]];

    //butt2與butt3水平對齊,豎直間隙20, 寬定為50

    NSDictionary *viewsDict = NSDictionaryOfVariableBindings(butt3,butt2);

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[butt3]-20-[butt2(==50)]" options:NSLayoutFormatAlignAllCenterX metrics:nil views:viewsDict]];

  }

  //A new view,相對于根視圖布局

  {

    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];

    view1.translatesAutoresizingMaskIntoConstraints = NO; //注意,代碼創(chuàng)建的view要設(shè)置此屬性為NO以防止系統(tǒng)自動轉(zhuǎn)換autoresizing

    view1.backgroundColor = [UIColor blueColor];

    [self.view addSubview:view1];

    //1.寬高比1:1.5

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeWidth multiplier:1.5f constant:0]];

    //2.固定高度150

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:0.f constant:150.f]];

    //3.水平居中 (3和4只能選其一,因為這兩個限制是沖突的)

    //  [self.view addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];

    //4.右邊對齊,保留10間隙

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1 constant:-10]];

    //5.上下間隔,這里要的效果是保持頂部間隙為100,所以底部間隙要是可變的>=100,試試下面6的效果對比一下

CGFloat hMargin = 100.f;

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-hmar-[view1]-(>=hmar)-|" options:0 metrics:@{@"hmar":@(hMargin)} views:NSDictionaryOfVariableBindings(view1)]];

    //6.上下間隔,頂部間隙可變

    //[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(>=100)-[view1]-(==100)-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view1)]];

  }

關(guān)于VFL,請參考蘋果官方文檔: Visual Format Language

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

當(dāng)前題目:iOS6+AutoLayout使用教程-創(chuàng)新互聯(lián)
網(wǎng)站URL:http://jinyejixie.com/article4/icgoe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)云服務(wù)器、做網(wǎng)站、網(wǎng)站制作、品牌網(wǎng)站制作網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站
陆良县| 开封市| 资中县| 泾川县| 吉木萨尔县| 周至县| 道真| 江油市| 浦北县| 云梦县| 金湖县| 习水县| 衡阳市| 女性| 南充市| 绍兴市| 嵩明县| 修武县| 碌曲县| 翁牛特旗| 泸州市| 宜兰市| 平遥县| 公主岭市| 嘉鱼县| 定陶县| 安仁县| 洛阳市| 霍山县| 涟水县| 朝阳市| 娱乐| 尼玛县| 临桂县| 平阴县| 图们市| 石城县| 红桥区| 芦山县| 和龙市| 渭源县|