VFL子视图居中
2015-09-08 13:01
337 查看
今天做UI用VFL适配在View上添加一个图片想让指定宽高的图片居中显示,我用下面的代码想着能实现可是出来的效果并没有居中。
后来在网上查了下终于找到解决方法
同理垂直居中也是
UIImageView *headView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"MR WU.png"]]; [headView setTranslatesAutoresizingMaskIntoConstraints:NO]; [self.view addSubview:headView]; [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-90-[headView(100)]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(headView)]]; [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[headView(100)]-|" options:NSLayoutFormatAlignAllCenterX metrics:nil views:NSDictionaryOfVariableBindings(headView)]];
后来在网上查了下终于找到解决方法
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:@"V:|-90-[headView(100)]" options:0 metrics:nil views:views]]; //设置宽度 [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[headView(100)]" options:0 metrics:nil views:views]]; //水平居中 [self.view addConstraint:[NSLayoutConstraint constraintWithItem:headView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
同理垂直居中也是
相关文章推荐
- Binary Tree Postorder Traversal
- UVA1630 - Folding
- 详解KMP算法中Next数组的求法
- 青蛙跳台阶
- Python 低通滤波器
- 非ROOT用户安装MySQL
- Leetcode ZigZag conversion
- Android里的一些小地方
- (转)Linux上的Shebang符号(#!)
- POJ 1041 - John's trip(欧拉回路)
- MongoDB学习日记(一):安装指南
- Python 读取WAV音频文件 画频谱
- Binary Tree Preorder Traversal
- layoutopt + Improving Your Code with lint
- VS2010上面遇到的两个错误
- IOS-友盟分享使用
- Android之CheckBox,RadioGroup(笔记)
- Java实现数组按数值大小排序
- RTB
- Java性能优化(12):最小化类和成员可访问能力