做登录页面,之前做都是用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. double? int?

    C# 值类型加上?表示可空类型(Nullable 结构),就是一种特殊的值类型,它的值可以为null 例: int? float? stirng? double?

  2. Android 推送

    安卓推送方案及比较 http://www.eoe.cn/news/11955.html ******************************************************** ...

  3. B - Bridging signals (LIS)

    点击打开链接 B - Bridging signals 'Oh no, they've done it again', cries the chief designer at the Waferlan ...

  4. “全栈2019”Java多线程第六章:中断线程interrupt()方法详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...

  5. kvm快照备份及常用命令

    转载自:http://www.myjishu.com/?p=431 好文章 kvm快照备份及常用命令 kvm快照,分两种: 1种lvm快照,如果分区是lvm,可以利用lvm进行kvm的快照备份 2种由 ...

  6. BZOJ 1248--游乐园(DFS&贪心)

    1248: 游乐园Pleasure Ground Time Limit: 10 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 6  Solved ...

  7. WC2019 全国模拟赛第一场 T1 题解

    由于只会T1,没法写游记,只好来写题解了... 题目链接 题目大意 给你一个数列,每次可以任取两个不相交的区间,取一次的贡献是这两个区间里所有数的最小值,求所有取法的贡献和,对 \(10^9+7\) ...

  8. 《Spark MLlib 机器学习实战》1——读后总结

    1 概念 2 安装 3 RDD RDD包含两种基本的类型:Transformation和Action.RDD的执行是延迟执行,只有Action算子才会触发任务的执行. 宽依赖和窄依赖用于切分任务,如果 ...

  9. 深入理解Scala的隐式转换

    摘要: 通过隐式转换,程序员可以在编写Scala程序时故意漏掉一些信息,让编译器去尝试在编译期间自动推导出这些信息来,这种特性可以极大的减少代码量,忽略那些冗长,过于细节的代码.   使用方式: 1. ...

  10. jquery input 实时监听输入

    $('input').bind('input propertychange', function() { alert('hello world') });