IOS布局笔记一(代码实现自己主动布局)
2023-09-27 14:25:17 时间
1.将一个试图放置在其父视图的中央位置,使用限制条件。
2.创建两个限制条件:一个是将目标视图的 center.x 位置排列在其父视图的 center.x 位置,而且另外一个是将目标视图的 center.y 位置排列在其父视图的 center.y 位置。
3.首先在 WildCatViewController.h中加入一个Button
//
// WildCatViewController.h
// AutoLayoutDemo
//
// Created by wildcat on 14-4-20.
// Copyright (c) 2014年 com.wildcat. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface WildCatViewController : UIViewController
@property(nonatomic,strong) UIButton*button;
@end
在.m文件里实现:
//
// WildCatViewController.m
// AutoLayoutDemo
//
// Created by wildcat on 14-4-20.
// Copyright (c) 2014年 com.wildcat. All rights reserved.
//
#import "WildCatViewController.h"@interface WildCatViewController ()
@end
@implementation WildCatViewController
@synthesize button=_button;
- (void)viewDidLoad
{
[super viewDidLoad];
_button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
_button.translatesAutoresizingMaskIntoConstraints=NO;
[_button setTitle:@"WildCat" forState:UIControlStateNormal];
[self.view addSubview:_button];
UIView *superView=_button.superview;
//加入约束,使按钮在屏幕水平方向的中央
NSLayoutConstraint *centerXContraint=[NSLayoutConstraint
constraintWithItem:_button
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:superView
attribute:NSLayoutAttributeCenterX
multiplier:1.0f
constant:0.0];
//加入约束。使按钮在屏幕垂直方向的中央
NSLayoutConstraint *centerYContraint=[NSLayoutConstraint
constraintWithItem:_button
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:superView
attribute:NSLayoutAttributeCenterY
multiplier:1.0f
constant:0.0];
//给button的父节点加入约束
[superView addConstraints:@[centerXContraint,centerYContraint]];
}
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskAll; //屏幕能够旋转
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
不要忘记更改设备能够旋转方向。
执行结果例如以下图:
本文转自:http://1.wildcat.sinaapp.com/?p=42
限制条件和他们要加入到的视图的关系图例如以下:
转载请注明:版权全部点击打开链接
接下来学什么:IOS布局笔记二( Visual Format Language 定义水平和垂直约束)
相关文章
- iOS 10 的 iTunes 备份极易破解?
- iMazing最新V2.15.6版本最好的 iOS 设备管理器
- IOS 多个UIImageView 加载高清大图时内存管理
- IOS学习笔记41--图片的缩放(一)
- iOS动画技术笔记
- iOS开发入门笔记
- IOS开发笔记 - 基于SDWebImage的网络图片加载处理
- 无限互联IOS电影项目视频笔记
- iOS AppStore 申请加急审核
- iOS 渲染相关面试题
- About RadaeePDF Classic SDK for iOS Crack
- ios真机调试错误解决:Installation of apps is prohibited by a policy on the device
- ios 初识开发概述(一)
- 斯坦福大学IOS开发课程笔记(第七课第二部分)
- IOS学习之斯坦福大学IOS开发课程笔记(第四课)
- IOS学习之斯坦福大学IOS开发课程笔记(第二课)
- IOS学习之斯坦福大学 IOS开发课程笔记(第一课)
- iOS 头文件导入@class注意事项
- 李洪强iOS经典面试题147-WebView与JS交互
- iOS学习笔记(2)— UIView用户事件响应
- iOS学习笔记(1)— UIView 渲染和内容管理
- 李洪强iOS开发之使用 Reachability 检测网络