IOS autoLayout 让视图居中
来源:互联网 发布:淘宝达人和微淘号达人 编辑:程序博客网 时间:2024/05/16 13:05
VFL的子视图居中,网上搜了很多都没找到有用代码,自己研究出来了。代码如下
- UIActivityIndicatorView* prgrssView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
- prgrssView.translatesAutoresizingMaskIntoConstraints = NO;
- [self.view addSubview:prgrssView];
- [prgrssView release];
- NSDictionary* views = NSDictionaryOfVariableBindings(prgrssView);
- [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-[prgrssView]-|" options:NSLayoutFormatAlignAllCenterX metrics:nil views:views]];
- [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[prgrssView]-|" options:NSLayoutFormatAlignAllCenterY metrics:nil views:views]];
以上代码测试下来只对UIActivityIndicatorView有效,如果一个view有宽度和高度则上面的居中代码会报错。可以通过另一种方式来处理,具体代码如下
- //beging
- UIImageView* imagevew = [UIImageView autolayoutView];
- [imagevew setContentMode:UIViewContentModeScaleToFill];
- [imagevew setImage:_tabCellImage];
- [imagevew setTag:AEC_TAG_IMAGE_VIEW];
- [self.view addSubview:imagevew];
- [imagevew release];
- NSDictionary* views = NSDictionaryOfVariableBindings(imagevew);
- //设置高度
- [self.view.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[imagevew(60)]" options:0 metrics:nil views:views]];
- //设置宽度
- [self.view.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[imagevew(60)]" options:0 metrics:nil views:views]];
- //垂直居中
- [self.view addConstraint:[NSLayoutConstraint constraintWithItem:imagevew attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
- //水平居中
- [self.view addConstraint:[NSLayoutConstraint constraintWithItem:imagevew attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
- //end
0 0
- IOS autoLayout 让视图居中
- IOS Autolayout(VFL) 处理子视图居中
- IOS Autolayout(VFL) 处理子视图居中
- IOS-AutoLayout居中
- 【iOS】UIScrollView中的UIImageView居中 与 autoLayout
- IOS让图片居中
- iOS编程(双语版)-视图-Autolayout代码初步
- iOS AutoLayout 设置多个相同间隔的视图
- (iOS开发)AutoLayOut中等比例设置视图
- iOS中让省略号垂直居中
- 让 ScrollView 中的视图底部组件,随ScrollView居中
- iOS: 让自定义控件适应Autolayout注意的问题
- iOS开发-一篇博客让你精通Autolayout适配
- ios AutoLayout
- ios AutoLayout
- iOS-AutoLayout
- IOS autolayout
- iOS AutoLayOut
- mac 首次 配置
- IT忍者神龟之使用HttpURLConnection向服务器发送post和get请求
- 数据结构2-动态生成顺序表的实例与分析
- ThinkPHP运算符 与 SQL运算符 对照表
- 【数据结构】顺序表的合并算法
- IOS autoLayout 让视图居中
- kindeditor-网页文字编辑
- 趋势科技安全威胁病毒预警(本周十大病毒排名)
- CentOS的利手:“Screen”一个可以在多个进程之间多路复用一个物理终端的窗口管理器
- IT忍者神龟之JSP禁用缓存常用方法
- HTML5未来发展的六大趋势
- fedora20 cocos2d-x 3.2 开发环境配置(for android)2
- php小写金额转大写
- 线程函数的设计以及MsgWaitForMultipleObjects函数的使用要点 ----- 转