IOS自动布局XIB和CODE,附代码和PPT

本文介绍了如何在iOS应用中使用Auto Layout进行视图控制器的界面布局。通过创建按钮并设置约束条件,实现了不同屏幕尺寸下的自适应布局。文中详细展示了如何使用视觉格式语言来定义视图间的间距和位置关系。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];

  4. UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
  5. btnBack.frame = CGRectMake(10, 10, 50, 30);
  6. [btnBack setImage:[UIImage imageNamed:@"sp_btn_back"] forState:UIControlStateNormal];
  7. [self.view addSubview:btnBack];

  8. UIButton *btnShare = [UIButton buttonWithType:UIButtonTypeCustom];
  9. btnShare.frame = CGRectMake(270, 10, 50, 30);
  10. [btnShare setImage:[UIImage imageNamed:@"sp_btn_share"] forState:UIControlStateNormal];
  11. [self.view addSubview:btnShare];

  12. UIButton *btnDetail = [UIButton buttonWithType:UIButtonTypeCustom];
  13. btnDetail.frame = CGRectMake(10, 380, 50, 30);
  14. [btnDetail setImage:[UIImage imageNamed:@"night_search_category_news"] forState:UIControlStateNormal];
  15. [self.view addSubview:btnDetail];

  16. UIButton *btnLikes = [UIButton buttonWithType:UIButtonTypeCustom];
  17. btnLikes.frame = CGRectMake(270, 380, 50, 30);
  18. [btnLikes setImage:[UIImage imageNamed:@"sp_btn_share"] forState:UIControlStateNormal];
  19. [self.view addSubview:btnLikes];

  20. [btnBack setTranslatesAutoresizingMaskIntoConstraints:NO];
  21. [btnShare setTranslatesAutoresizingMaskIntoConstraints:NO];
  22. [btnDetail setTranslatesAutoresizingMaskIntoConstraints:NO];
  23. [btnLikes setTranslatesAutoresizingMaskIntoConstraints:NO];

  24. NSDictionary *views = NSDictionaryOfVariableBindings(self.view, btnBack, btnShare, btnDetail, btnLikes);
  25. [self.view addVisualConstraints:@"|-10-[btnBack]" forViews:views];
  26. [self.view addVisualConstraints:@"[btnShare]-10-|" forViews:views];
  27. [self.view addVisualConstraints:@"|-10-[btnDetail]" forViews:views];
  28. [self.view addVisualConstraints:@"V:[btnDetail]-10-|" forViews:views];
  29. [self.view addVisualConstraints:@"[btnLikes]-10-|" forViews:views];
  30. [self.view addVisualConstraints:@"V:[btnLikes]-10-|" forViews:views];

  31. }
复制代码
Uiview扩展代码
  1. #import "UIView+Constraint.h"

  2. @implementation UIView (Constraint)

  3. - (void)addVisualConstraints:(NSString *)constraintString forViews:(NSDictionary *)views {
  4. [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:constraintString
  5. options:0
  6. metrics:0
  7. views:views]];
  8. }

  9. @end
复制代码
Visual Format Syntax
The following are examples of constraints you can specify using the visual format. Note how the text visually matches the image.
Standard Space 
[button]-[textField]
  Width Constraint 
[button(>=50)]
  Connection to Superview 
|-50-[orchidBox]-50-|
  Vertical Layout 
V:[topField]-10-[bottomField]
  Flush Views 
[maroonView][oceanView]
  Priority 
[button(100@20)]
  Equal Widths 
[button1(==button2)]
  Multiple Predicates 
[flexibleButton(>=70,<=100)]
  A Complete Line of Layout 
|-[find]-[findNext]-[findField(>=20)]-|
The notation prefers good visualization over completeness of expressibility. There are constraints that cannot be expressed in visual format syntax, although most of the constraints that are useful in real user interfaces can be. One useful constraint that cannot be expressed is a fixed aspect ratio (for example,imageView.width = 2 * imageView.height). To create such a constraint, you must use constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:.

Visual Format String Grammar
The visual format string grammar is defined as follows (literals are shown in code font; e denotes the empty string).
Symbol
Replacement rule

<visualFormatString>
(<orientation>:)?
(<superview><connection>)?
<view>(<connection><view>)*
(<connection><superview>)?

<orientation>
H|V

<superview>
|

<view>
[<viewName>(<predicateListWithParens>)?]

<connection>
e|-<predicateList>-|-

<predicateList>
<simplePredicate>|<predicateListWithParens>

<simplePredicate>
<metricName>|<positiveNumber>

<predicateListWithParens>
(<predicate>(,<predicate>)*)

<predicate>
(<relation>)?(<objectOfPredicate>)(@<priority>)?

<relation>
==|<=|>=

<objectOfPredicate>
<constant>|<viewName>  (see note)

<priority>
<metricName>|<number>

<constant>
<metricName>|<number>

<viewName>
Parsed as a C identifier.
This must be a key mapping to an instance of NSView in the passed views dictionary.

<metricName>
Parsed as a C identifier. This must be a key mapping to an instance of NSNumber in the passed metrics dictionary.

<number>
As parsed by strtod_l, with the C locale.

Note: For the objectOfPredicate production, a viewName is only acceptable if the subject of the predicate is the width or height of a view. That is, you can use[view1(==view2)] to specify that view1 and view2 have the same width.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值