做登录页面,之前做都是用frame做,今天想着用Auto Layout中的VFL来做。觉得做的效果还是可以的(自恋一下下)。

首先看下效果图和标记图

自己在做的过程中也遇到了好多问题,不过也一个一个的自己解决了

1.子视图居中的问题

上一博客我也写了,由于指定了视图的宽度高度,想让视图居中对齐,可它就是不能达到预期,最后还是网上找了下才解决的。

2.约束不起作用

引起这个问题的原因很多,其中有一个是犯的最愚蠢的错误,就是添加约束前设置子视图

setTranslatesAutoresizingMaskIntoConstraintsNO.

setTranslatesAutoresizingMaskIntoConstraints=NO.

setTranslatesAutoresizingMaskIntoConstraints=NO

重要的事情说三遍

-------------------华丽的分割线------------------------------------

上面说的适配,这里说下实现

根据上图有一个账号 一个密码,它们两个只是图片 名称和文本类型不一样其他都一样,所以我就把它封装成一个View。

//
//  LoginView.h
//  Login
//
//  Created by City--Online on 15/9/8.
//  Copyright (c) 2015年 City--Online. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface LoginView : UIView
@property (nonatomic,strong) UIImageView *leftImgView;
@property (nonatomic,strong) UILabel *nameLabel;
@property (nonatomic,strong) UITextField *txtField;
@property (nonatomic,strong) UIView *bottomLine;
@end
//
//  LoginView.m
//  Login
//
//  Created by City--Online on 15/9/8.
//  Copyright (c) 2015年 City--Online. All rights reserved.
//

#import "LoginView.h"

@implementation LoginView
-(instancetype)init
{
    self=[super init];
    if (self) {

        [self setTranslatesAutoresizingMaskIntoConstraints:NO];
        //左边图片
        _leftImgView=[[UIImageView alloc]init];
        [_leftImgView setTranslatesAutoresizingMaskIntoConstraints:NO];
        [self addSubview:_leftImgView];

        //名称
        _nameLabel=[[UILabel alloc]init];
        _nameLabel.font=[UIFont systemFontOfSize:18.0];
        _nameLabel.textColor=[UIColor colorWithRed:0.200f green:0.200f blue:0.200f alpha:1.00f];
        [_nameLabel setTranslatesAutoresizingMaskIntoConstraints:NO];

        [self addSubview:_nameLabel];

        //输入文本框
        _txtField=[[UITextField alloc]init];
        _txtField.font=[UIFont systemFontOfSize:];
//        _txtField.layer.borderWidth=2.0;
        [_txtField setTranslatesAutoresizingMaskIntoConstraints:NO];

        [self addSubview:_txtField];

        //底部线条
        _bottomLine=[[UIView alloc]init];
        _bottomLine.backgroundColor=[UIColor colorWithRed:0.427f green:0.427f blue:0.427f alpha:1.00f];
        [_bottomLine setTranslatesAutoresizingMaskIntoConstraints:NO];
        [self addSubview:_bottomLine];
    }
    return self;
}
-(void)updateConstraints
{
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat: metrics:nil views:NSDictionaryOfVariableBindings(_leftImgView,_nameLabel,_txtField)]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat: metrics:nil views:NSDictionaryOfVariableBindings(_leftImgView,_bottomLine)]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat: metrics:nil views:NSDictionaryOfVariableBindings(_bottomLine)]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat: metrics:nil views:NSDictionaryOfVariableBindings(_txtField,_bottomLine)]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat: metrics:nil views:NSDictionaryOfVariableBindings(_nameLabel)]];
    [super updateConstraints];
}

@end

在界面中布局登录页面:

//
//  ViewController.m
//  Login
//
//  Created by City--Online on 15/9/8.
//  Copyright (c) 2015年 City--Online. All rights reserved.
//

#import "ViewController.h"
#import "LoginView.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor=[UIColor whiteColor];

    //顶部图片
    UIImageView *headView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"MR WU.png"]];
    [headView setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.view addSubview:headView];

    NSDictionary* views = NSDictionaryOfVariableBindings(headView);
    //设置高度
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat: metrics:nil views:views]];
    //设置宽度
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat: metrics:nil views:views]];
    //水平居中
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:headView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier: constant:]];

    //账号
    LoginView *accountView=[[LoginView alloc]init];
    accountView.leftImgView.image=[UIImage imageNamed:@"user.png"];
    accountView.nameLabel.text=@"账号";
    accountView.txtField.secureTextEntry=NO;
    [self.view addSubview:accountView];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat: metrics:nil views:NSDictionaryOfVariableBindings(accountView)]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat: metrics:nil views:NSDictionaryOfVariableBindings(headView,accountView)]];

    //密码
    LoginView *passWordView=[[LoginView alloc]init];
    passWordView.leftImgView.image=[UIImage imageNamed:@"lock-"];
    passWordView.nameLabel.text=@"密码";
    passWordView.txtField.secureTextEntry=YES;
    [self.view addSubview:passWordView];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat: metrics:nil views:NSDictionaryOfVariableBindings(passWordView)]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat: metrics:nil views:NSDictionaryOfVariableBindings(accountView,passWordView)]];

    //登录按钮
    UIButton *loginBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [loginBtn setTranslatesAutoresizingMaskIntoConstraints:NO];
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [loginBtn setTitleColor:[UIColor colorWithRed:1.000f green:1.000f blue:1.000f alpha:1.00f] forState:UIControlStateNormal];
    loginBtn.titleLabel.font=[UIFont systemFontOfSize:];
    loginBtn.backgroundColor=[UIColor colorWithRed:0.992f green:0.318f blue:0.106f alpha:1.00f];
    [self.view addSubview:loginBtn];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat: metrics:nil views:NSDictionaryOfVariableBindings(passWordView,loginBtn)]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat: metrics:nil views:NSDictionaryOfVariableBindings(loginBtn)]];

    UILabel *titleLabel=[[UILabel alloc]init];
    [titleLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
    titleLabel.text=@"胖吴货栈发货系统";
    titleLabel.font=[UIFont systemFontOfSize:14.0];
    [titleLabel setTextColor:[UIColor colorWithRed:0.992f green:0.318f blue:0.106f alpha:1.00f]];
    [self.view addSubview:titleLabel];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[titleLabel]" options:NSLayoutFormatAlignAllCenterX metrics:nil views:NSDictionaryOfVariableBindings(titleLabel)]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat: metrics:nil views:NSDictionaryOfVariableBindings(titleLabel)]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:titleLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier: constant:]];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

@end

上面代码中包含了VFL的基本语法,对于更深的语法慢慢的来了解,晒一下做的效果

4s

5

5s

6

6+

前面用的autoLayout自动布局,想着是自动布局Frame会不起作用,一直纠结键盘遮挡的问题。在网上找了下,有网友说改变约束,自己的布局也正好是以顶部的图片依次相对布局 ,所以准备改下顶部的约束就会解决键盘遮挡。自己定了一个标记值,判断键盘隐藏顶部约束值为正,否则为负。可是问题出现了,点击文本框后能键盘弹出后视图能向上,但键盘隐藏之后视图并未回到原位,自己又试着删除全部约束重新添加可还是不行,这个问题让我纠结好久。最后问了下小伙伴oliver,改了下frame问题就解决了。我以为用autolayout并未设置frame,怎么会有呢,问了他下,原来适配也是有frame的。这让我想起了autoLayout的原理,它属于数学的线性规划。最终还是会有一个值的

IOS使用Auto Layout中的VFL适配的更多相关文章

  1. 使用Auto Layout中的VFL(Visual format language)--代码实现自动布局

    使用Auto Layout中的VFL(Visual format language)--代码实现自动布局 2014-12-09 10:56 编辑: zhiwupei 分类:iOS开发 来源:机智的新手 ...

  2. 使用Auto Layout中的VFL(Visual format language)--代码实现自动布局【转】

    本文将通过简单的UI来说明如何用VFL来实现自动布局.在自动布局的时候避免不了使用代码来加以优化以及根据内容来实现不同的UI. 一:API介绍 NSLayoutConstraint API 1 2 3 ...

  3. 转载自@机智的新手:使用Auto Layout中的VFL(Visual format language)--代码实现自动布局

    本文将通过简单的UI来说明如何用VFL来实现自动布局.在自动布局的时候避免不了使用代码来加以优化以及根据内容来实现不同的UI. 一:API介绍 NSLayoutConstraint API 1 2 3 ...

  4. 【转】使用Auto Layout中的VFL(Visual format language)--代码实现自动布局

    本文将通过简单的UI来说明如何用VFL来实现自动布局.在自动布局的时候避免不了使用代码来加以优化以及根据内容来实现不同的UI. 一:API介绍 NSLayoutConstraint API 1 2 3 ...

  5. 转载:使用Auto Layout中的VFL(Visual format language)--代码实现自动布局

    本文将通过简单的UI来说明如何用VFL来实现自动布局.在自动布局的时候避免不了使用代码来加以优化以及根据内容来实现不同的UI. 一:API介绍 NSLayoutConstraint API 1 2 3 ...

  6. 使用Auto Layout中的VFL(Visual format language)——代码实现自动布局

    本文将通过简单的UI来说明如何用VFL来实现自动布局.在自动布局的时候避免不了使用代码来加以优化以及根据内容来实现不同的UI. 一:api介绍 1.NSLayoutConstraint API NSL ...

  7. [Android开发学iOS系列] Auto Layout

    [Android开发学iOS系列] Auto Layout 内容: 介绍什么是Auto Layout. 基本使用方法 在代码中写约束的方法 Auto Layout的原理 尺寸和优先级 Auto Lay ...

  8. iOS Programming Auto Layout: Programmatic Constraints 自动布局:通过编程限制

    iOS Programming  Auto Layout: Programmatic Constraints  1.  However, if your views are created in co ...

  9. iOS 7 - Auto Layout on iOS Versions prior to 6.0

    链接地址:http://stackoverflow.com/questions/18735847/ios-7-auto-layout-on-ios-versions-prior-to-6-0 Stac ...

随机推荐

  1. DevExpress GridControl+UserControl实现分页

    志向不过是记忆的奴隶,生气勃勃地降生,但却很难成长. —— 莎士比亚 时隔一年,我写随笔真的很随意,想起了就来博客园写写,想不起来就任由懒惰支配着我.不过我到觉得这不是什么坏事,你不用为了完成某事而让 ...

  2. 关于mysql中[Err] 1451 -Cannot delete or update a parent row: a foreign key constraint fails

    mysql> SET FOREIGN_KEY_CHECKS = 0; Query OK, 0 rows affected (0.02 sec)   mysql> delete from r ...

  3. 【Oracle 12c】CUUG OCP认证071考试原题解析(32)

    32.choose the best answer View the Exhibit and examine the data in EMP and DEPT tables. In the DEPT ...

  4. leetcode-49-字母异位词分组(神奇的哈希)

    题目描述: 给定一个字符串数组,将字母异位词组合在一起.字母异位词指字母相同,但排列不同的字符串. 示例: 输入: ["eat", "tea", "t ...

  5. QuantLib 金融计算——基本组件之 InterestRate 类

    目录 QuantLib 金融计算--基本组件之 InterestRate 类 InterestRate 对象的构造 一些常用的成员函数 如果未做特别说明,文中的程序都是 Python3 代码. Qua ...

  6. centos6.5安装docker(亲测)

    centos6.5下安装docker的过程办法 在看了网上N多复制粘贴的文章,又尝试无效后,我把我最终成功的办法发出来,希望能帮到拼命干环境的你. 操作环境: centos6.5(Final) 内核: ...

  7. Springboot第五篇:结合myBatis进行SQL操作

    前提:和之前同样的,本篇会从前端和后台一起讲述关于SQL的select操作(其他操作原理大致类似,不多做解释了). 大致流程:前端通过AJAX将数据发送到后台的路由,后台路由会根据发送的数据进行SQL ...

  8. LinkedList简要分析

    LinkedList概述 LinkedList 实现List接口,底层是双向链表,非线程安全.LinkedList还可以被当作堆栈.队列或双端队列进行操作.在JDK1.7/8 之后取消了循环,修改为双 ...

  9. windows phpinfo上不能找到memcache扩展 php版本5.6

    我的memcache用的我是memcached-win64-1.4.4-14.zip这个版本memcache扩展库下载地址:http://windows.php.net/downloads/pecl/ ...

  10. php 判断两个时间段是否有交集

    一开始,没啥思路,全靠百度,记录一下哈 public function demo(){ //例子 $astart = strtotime("1995-06-16 12:00:00" ...