iOS自动布局
iOS 自动布局(Auto Layout)是Apple在iOS 6及以后版本引入的一种动态布局系统,用于处理用户界面元素在不同屏幕尺寸和方向下的自适应布局。通过自动布局,开发者可以创建灵活的、可伸缩的UI设计,确保应用在各种iPhone、iPad设备以及不同的屏幕旋转状态下都能保持良好的视觉效果和用户体验。
以下是iOS自动布局的主要特点和使用方法:
约束(Constraints):自动布局的核心是约束,它定义了视图间的关系(如距离、对齐、宽高比等)。开发者可以通过代码或Interface Builder为每个视图添加一组约束来指定其相对于父视图或其他视图的位置和大小。
可视化格式语言(Visual Format Language, VFL):一种简洁的字符串语法,用于快速描述复杂布局关系。
NSLayoutAnchor和NSLayoutConstraint:在Swift中,苹果推荐使用NSLayoutAnchor类及其子类(如UIView的leadingAnchor、topAnchor等)来更安全且方便地创建约束。
优先级(Priority):约束具有优先级设置,允许开发者定义哪些约束必须满足,哪些可以在空间不足时适当调整。
更新约束(Update Constraints):当需要动态改变布局时,可以通过更新现有约束或者添加/删除约束来响应内容的变化或设备状态变化。
UIScrollView 和 Auto Layout 结合:从iOS 11开始,UIScrollView新增了一个
contentLayoutGuide
属性,使得在滚动视图内使用自动布局更加容易,能够正确管理内容视图与滚动区域的关系。Stack Views(堆栈视图):自iOS 9起,UIStackView作为自动布局的辅助工具被引入,它可以管理和排列一组子视图,并根据约束自动调整它们的位置和大小。
通过合理运用自动布局,开发者可以避免手动计算和硬编码坐标,使应用程序更容易维护和适配未来的设备屏幕变化。
实例步骤
1.创建一个简单的 View based application
2.修改 ViewController.m 的文件内容,如下所示
#import "ViewController.h" @interface ViewController () @property (nonatomic, strong) UIButton *leftButton; @property (nonatomic, strong) UIButton *rightButton; @property (nonatomic, strong) UITextField *textfield; @end @implementation ViewController - (void)viewDidLoad{ [super viewDidLoad]; UIView *superview = self.view; /*1. Create leftButton and add to our view*/ self.leftButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; self.leftButton.translatesAutoresizingMaskIntoConstraints = NO; [self.leftButton setTitle:@"LeftButton" forState:UIControlStateNormal]; [self.view addSubview:self.leftButton]; /* 2. Constraint to position LeftButton's X*/ NSLayoutConstraint *leftButtonXConstraint = [NSLayoutConstraint constraintWithItem:self.leftButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:superview attribute: NSLayoutAttributeCenterX multiplier:1.0 constant:-60.0f]; /* 3. Constraint to position LeftButton's Y*/ NSLayoutConstraint *leftButtonYConstraint = [NSLayoutConstraint constraintWithItem:self.leftButton attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:superview attribute: NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f]; /* 4. Add the constraints to button's superview*/ [superview addConstraints:@[ leftButtonXConstraint, leftButtonYConstraint]]; /*5. Create rightButton and add to our view*/ self.rightButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; self.rightButton.translatesAutoresizingMaskIntoConstraints = NO; [self.rightButton setTitle:@"RightButton" forState:UIControlStateNormal]; [self.view addSubview:self.rightButton]; /*6. Constraint to position RightButton's X*/ NSLayoutConstraint *rightButtonXConstraint = [NSLayoutConstraint constraintWithItem:self.rightButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:superview attribute: NSLayoutAttributeCenterX multiplier:1.0 constant:60.0f]; /*7. Constraint to position RightButton's Y*/ rightButtonXConstraint.priority = UILayoutPriorityDefaultHigh; NSLayoutConstraint *centerYMyConstraint = [NSLayoutConstraint constraintWithItem:self.rightButton attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:superview attribute: NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f]; [superview addConstraints:@[centerYMyConstraint, rightButtonXConstraint]]; //8. Add Text field self.textfield = [[UITextField alloc]initWithFrame: CGRectMake(0, 100, 100, 30)]; self.textfield.border = UITextBorderRoundedRect; self.textfield.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:self.textfield]; //9. Text field Constraints NSLayoutConstraint *textFieldTopConstraint = [NSLayoutConstraint constraintWithItem:self.textfield attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:superview attribute:NSLayoutAttributeTop multiplier:1.0 constant:60.0f]; NSLayoutConstraint *textFieldBottomConstraint = [NSLayoutConstraint constraintWithItem:self.textfield attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:self.rightButton attribute:NSLayoutAttributeTop multiplier:0.8 constant:-60.0f]; NSLayoutConstraint *textFieldLeftConstraint = [NSLayoutConstraint constraintWithItem:self.textfield attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:superview attribute: NSLayoutAttributeLeft multiplier:1.0 constant:30.0f]; NSLayoutConstraint *textFieldRightConstraint = [NSLayoutConstraint constraintWithItem:self.textfield attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:superview attribute: NSLayoutAttributeRight multiplier:1.0 constant:-30.0f]; [superview addConstraints:@[textFieldBottomConstraint , textFieldLeftConstraint, textFieldRightConstraint, textFieldTopConstraint]]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
原文链接: https://www.yukx.com/bingningm/article/details/720.html 优科学习网iOS自动布局
-
在iOS开发中,要实现发送电子邮件的功能,可以使用MFMailComposeViewController类,这是Apple提供的一个内置框架——MessageUI的一部分。以下是在Swift中使用MFMailComposeViewController来发送邮件的基本步骤:实例步骤1、创建一个简单的V
-
iOS故事板(Storyboards)是Apple在iOS5中引入的一种可视化界面构建工具,它允许开发者在一个单一的文件中设计和管理整个应用程序的所有用户界面屏幕以及它们之间的导航关系。故事板通过InterfaceBuilder集成到Xcode开发环境中,为开发者提供了一种直观的方式来组织UI元素、
-
iOS自动布局(AutoLayout)是Apple在iOS6及以后版本引入的一种动态布局系统,用于处理用户界面元素在不同屏幕尺寸和方向下的自适应布局。通过自动布局,开发者可以创建灵活的、可伸缩的UI设计,确保应用在各种iPhone、iPad设备以及不同的屏幕旋转状态下都能保持良好的视觉效果和用户体验
-
ios(InternetworkingOperatingSystem-Cisco,缩写ios,也可写作IOS,区别于苹果系统iOS)iOS是由苹果公司(AppleInc.)开发的一种专有移动操作系统,它为苹果的多款移动设备提供用户界面和底层操作逻辑。最初随iPhone在2007年发布时被称为“iPh
-
小程序中的组件也是由宿主环境提供的,开发者可以使用组件快速搭建出页面结构,官方把小程序里的组件分为了9大类,分别是视图容器基础内容表单组件导航组件媒体组件map地图组件canvas画布组件开放能力无障碍访问常见的视图容器类组件1.view普通视图区域类似于HTML中的div,是一个块级元素常用于实现
-
一、下载并安装开发工具uniapp的开发工具为HbuilderX,下载地址为:去下载这里选择windows版本。windows版本下载完成之后会得到一个zip的压缩包文件,解压完成即可使用,是不需要安装的绿色版本。二、安装sass和scss插件打开HbuilderX,点击工具----插件安装,如图所