首页 技术 正文
技术 2022年11月14日
0 收藏 573 点赞 2,358 浏览 4534 个字

参考资料https://developer.apple.com/library/mac/documentation/UserExperience/Conceptual/AutolayoutPG/VisualFormatLanguage/VisualFormatLanguage.html#//apple_ref/doc/uid/TP40010853-CH3https://developer.apple.com/library/ios/technotes/tn2154/_index.htmlhttps://developer.apple.com/library/mac/documentation/AppKit/Reference/NSLayoutConstraint_Class/NSLayoutConstraint/NSLayoutConstraint.html开源项目Masonry旨在让自动布局(Auto Layout)的代码更简洁、可读性更强,下面是它的地址,个人认为在开发过程中使用Masonry可以是代码更加友好https://github.com/cloudkite/Masonry 

- (void)viewDidLoad
{
[super viewDidLoad]; [self addNavigationBar:FEEDBACK_VIEW_CONTROLLER]; UILabel *note = [[UILabel alloc] init];
[note setText:@"欢迎提出宝贵意见!您留下的每一份心意都将浇灌母婴宝的茁壮成长。"];
[note setLineBreakMode:NSLineBreakByWordWrapping];
note.numberOfLines = 0;
[self.view addSubview:note]; //将自适应向布局约束的转化关掉(根据情况有时需要有时不需要)
[note setTranslatesAutoresizingMaskIntoConstraints:NO]; [self.view addConstraint:[NSLayoutConstraint constraintWithItem:note
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:navBar
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:10]]; [self.view addConstraint:[NSLayoutConstraint constraintWithItem:note
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:10]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:note
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeRight
multiplier:1.0
constant:-10]];
UITextView *tfContent = [[UITextView alloc] init];
[tfContent setTranslatesAutoresizingMaskIntoConstraints:NO];
[tfContent setBackgroundColor:[UIColor whiteColor]];
[tfContent setReturnKeyType:UIReturnKeyDone];
[self.view addSubview:tfContent]; [self.view addConstraint:[NSLayoutConstraint constraintWithItem:tfContent
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:note
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:10]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:tfContent
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:10]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:tfContent
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeRight
multiplier:1.0
constant:-10]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:tfContent
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:100]]; UITextField *tfEmail = [UITextField new];
[tfEmail setTranslatesAutoresizingMaskIntoConstraints:NO];
[tfEmail setBackgroundColor:[UIColor whiteColor]];
[tfEmail setClearButtonMode:UITextFieldViewModeAlways];
[tfEmail setReturnKeyType:UIReturnKeyDone];
[tfEmail setPlaceholder:@"请输入邮箱,以便我们联系你!"];
[self.view addSubview:tfEmail]; [self.view addConstraint:[NSLayoutConstraint constraintWithItem:tfEmail
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:tfContent
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:10]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:tfEmail
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:10]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:tfEmail
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeRight
multiplier:1.0
constant:-10]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:tfEmail
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:35]]; UIButton *btnSubmit = [[UIButton alloc] init];
[btnSubmit setTranslatesAutoresizingMaskIntoConstraints:NO];
[btnSubmit setTitle:STRING_COMMON_SUBMIT forState:UIControlStateNormal];
[btnSubmit setBackgroundImage:[UIImage imageNamed:@"bg_red_orange"] forState:UIControlStateNormal];
[btnSubmit setBackgroundImage:[UIImage imageNamed:@"bg_red"] forState:UIControlStateHighlighted];
[self.view addSubview:btnSubmit]; NSMutableArray *tmpConstraints = [NSMutableArray array]; [tmpConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[tfEmail]-10-[btnSubmit(==35)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(tfEmail,btnSubmit)]];
[tmpConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[btnSubmit]-10-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(btnSubmit)]];
[self.view addConstraints:tmpConstraints];}

  IOS自动布局

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,491
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,492
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,293