• IOS学习之路(代码实现自动布局)


    1.将一个试图放置在其父视图的中央位置,使用限制条件。
    2.创建两个限制条件:一个是将目标视图的 center.x 位置排列在其父视图的 center.x 位置,并且另外一个是将目标视图的 center.y 位置排列在其父视图的 center.y 位置。
    3.首先在 WildCatViewController.h中添加一个Button
    //
    // WildCatViewController.h
    // AutoLayoutDemo
    //
    // Created by wildcat on 14-4-20.
    // Copyright (c) 2014年 com.wildcat. All rights reserved.
    //
    #import <UIKit/UIKit.h>
    @interface WildCatViewController : UIViewController
    @property(nonatomic,strong) UIButton*button;
    @end
    在.m文件中实现:

    //
    // WildCatViewController.m
    // AutoLayoutDemo
    //
    // Created by wildcat on 14-4-20.
    // Copyright (c) 2014年 com.wildcat. All rights reserved.
    //
    #import "WildCatViewController.h"@interface WildCatViewController ()
    @end
    
    @implementation WildCatViewController
    @synthesize button=_button;
    - (void)viewDidLoad
    {
    [super viewDidLoad];
    _button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    _button.translatesAutoresizingMaskIntoConstraints=NO;
    [_button setTitle:@"WildCat" forState:UIControlStateNormal];
    [self.view addSubview:_button];
    
    UIView *superView=_button.superview;
    //添加约束,使按钮在屏幕水平方向的中央
    NSLayoutConstraint *centerXContraint=[NSLayoutConstraint
    constraintWithItem:_button
    attribute:NSLayoutAttributeCenterX
    relatedBy:NSLayoutRelationEqual
    toItem:superView
    attribute:NSLayoutAttributeCenterX
    multiplier:1.0f
    constant:0.0];
    //添加约束,使按钮在屏幕垂直方向的中央
    NSLayoutConstraint *centerYContraint=[NSLayoutConstraint
    constraintWithItem:_button
    attribute:NSLayoutAttributeCenterY
    relatedBy:NSLayoutRelationEqual
    toItem:superView
    attribute:NSLayoutAttributeCenterY
    multiplier:1.0f
    constant:0.0];
    //给button的父节点添加约束
    [superView addConstraints:@[centerXContraint,centerYContraint]];
    
    }
    
    -(NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskAll; //屏幕可以旋转
    }
    
    - (void)didReceiveMemoryWarning
    {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }
    
    @end

    不要忘记更改设备可以旋转方向,
    运行结果如下图:

    本文转自:http://1.wildcat.sinaapp.com/?p=42

    限制条件和他们要添加到的视图的关系图如下:

     

     

    layout

     转载请注明:版权所有http://1.wildcat.sinaapp.com/

    未完待续……


  • 相关阅读:
    CSS3 box-shadow(阴影使用)
    缩小窗口时CSS背景图出现右侧空白BUG的解决方法
    html打开个人QQ聊天页面
    帮助你实现元素动画的6款插件
    给出两个颜色,计算中间颜色返回数组
    名字首字母
    自适应网页设计
    tab切换jquery代码
    改变上传文件样式
    剑指offer 面试16题
  • 原文地址:https://www.cnblogs.com/lixingle/p/3707687.html
Copyright © 2020-2023  润新知