UI一揽子计划 3 (自定义视图、UIViewControllor、屏幕旋转判断和重布局)
2.把想要的视图 在子类中创建并添加(在初始化方法中添加)
// 创建属性 开放接口 方便外界访问
@property (nonatomic,retain)UILabel *label;
@property (nonatomic,retain)UITextField *textField;
self = [super initWithFrame:frame];
if (self) {
/**
* 添加自定义视图的各种视图了
*/
// 得到宽度和高度
CGFloat width = frame.size.width;
CGFloat height = frame.size.height;
_label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, width/3-20, height)];
// _label.backgroundColor = [UIColor greenColor];
_textField = [[UITextField alloc]initWithFrame:CGRectMake(width/3, 0, width/3*2, height)];
_textField.backgroundColor = [UIColor grayColor];
// 将Label textfield 添加到自己身上
[self addSubview:_label];
[self addSubview:_textField];
[_label release];
// 如果已经更改了frame 就会触发 一转就改变了frame,就触发了该方法 那么我就可以在这里更改布局。
- (void)layoutSubviews
{
[super layoutSubviews];
// 判断了方向 就可以更改布局了
//[UIApplication sharedApplication] 代表应用程序
// statusBarOrientation 代表设备的方向
// UIInterfaceOrientationPortrait 正着的方向
// UIInterfaceOrientationPortraitUpsideDown 倒着的方向
if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown) {
NSLog(@"竖屏");
self.passwordView.frame = CGRectMake(self.userNameView.frame.origin.x, self.userNameView.frame.origin.y + 40 + 20, 300, 40);
} else {
NSLog(@"横屏");
// 重新对横屏 进行布局 首先应该知道它原来是怎么写的
self.passwordView.frame = CGRectMake(self.userNameView.frame.origin.x + 300 + 20, 100, 300, 40);
* 屏幕旋转的步骤
1. 允许旋转
2. 允许各个方向
3. 获取横屏还是竖屏
*/
// 是否允许旋转允许旋转
- (BOOL)shouldAutorotate // 旋转 rotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
// 判断是否是横屏
// 划了横线 2.0-8.0 可以用,禁用 或者不推荐用
//- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
//{
//
//}
// 官方推荐我们8.0之后用这个方法
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
// 将size转化成字符串打印出来
NSLog(@"%@", NSStringFromCGSize(size));
/*
2015-08-25 17:45:37.979 LessonLTView[4161:214251] {667, 375}
2015-08-25 17:45:40.618 LessonLTView[4161:214251] {375, 667}
2015-08-25 17:45:42.686 LessonLTView[4161:214251] {667, 375}
2015-08-25 17:45:44.203 LessonLTView[4161:214251] {375, 667}
*/
{
// 如果使用以下的2 选择的话就注释掉这句。就不会崩溃 不然就崩溃 因为没调用父类的方法(方法本身就是加载view的),相当于给一个空的东西赋值。所以一般采用1 选择,直接进行赋值。
// [super loadView];
// 它要一个view 咱就给它一个
LTBView *login = [[LTBView alloc]initWithFrame:[UIScreen mainScreen].bounds];
// 以下两种选择 1. 直接赋值给self.view
self.view = login;
[login release];
// 2. 加到self.view 上
// [self.view addSubview:login];
/**
* 如果还想在上面添加控件的话 最好在《视图加载完毕》(viewDidLoad)的方法中添加。
*/
// [super viewDidLoad];
/**
* 把 登陆的View 取出来
*/
LTBView *loginView = (LTBView *)self.view;
[loginView.loginButton addTarget:self action:@selector(actionLoginButtton:) forControlEvents:(UIControlEventTouchUpInside)];
{
NSLog(@"将要显示");
}
- (void)viewWillDisappear:(BOOL)animated
{
NSLog(@"将要消失");
}
- (void)viewDidAppear:(BOOL)animated
{
NSLog(@"已经显示");
}
- (void)viewDidDisappear:(BOOL)animated
{
NSLog(@"已经消失");
// 把视图销毁。
// self.view = nil;
if ([self isViewLoaded] == YES && self.view.window == nil) { // 如果视图已经被加载过且视图不是当前在显示的 那么我们才能把视图销毁
// 把不用的视图销毁了
self.view = nil;
self.view.backgroundColor = [UIColor yellowColor];
UIButton *button1 = [UIButton buttonWithType:(UIButtonTypeCustom)];
[button1 setTitle:@"返回" forState:(UIControlStateNormal)];
[button1 setTitle:@"返回" forState:(UIControlStateHighlighted)];
button1.backgroundColor = [UIColor redColor];
button1.frame = CGRectMake(20, 20, 60, 40);
[button1 setTitleColor:[UIColor blueColor] forState:(UIControlStateNormal)];
[button1 setTitleColor:[UIColor grayColor] forState:(UIControlStateHighlighted)];
[button1 addTarget:self action:@selector(action:) forControlEvents:(UIControlEventTouchUpInside)];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)action:(UIButton *)button
{
2.设计样式 主题 self 取消 确定 第一个按钮 第二个按钮 从下往上0-N actionsheet。actionsheetstyle = (枚举类型的样式)。 actionsheet showInView self.windows
3.代理方法 if (buttonIndex == 0){ [self showAlert :@"确定"]} else if () {} 再写一个UIAlertView 方法
UI一揽子计划 3 (自定义视图、UIViewControllor、屏幕旋转判断和重布局)相关推荐
- UI一揽子计划 21 (UICollectionView、SDWebImage第三方类库加载图片的使用、集合视图的布局UICollectionViewFlowLayout 、自定义Cell、布局协议
Pro : SDWebImage第三方类库加载图片的使用: 1.在MRC 环境下 使用ARC 的类库: -fobjc-arc (Build Phases --> Compile Sourc ...
- UI一揽子计划 11 (自定义UITableViewCell、Cell 的自适应高度)
一. 自定义UITableViewCell 在日常的编程中,系统提供的几种Cell 样式 往往不能满足我们的需求.所以需要我们给它进行自定义样式. 自定义Cell 就是创建一个UITableViewC ...
- UI一揽子计划 9 (UITableView 、UITableView 、重用机制)
一. UITableView UITableView继承自UIScrollView,所以可以滚动 表视图的每⼀一条数据都是显示在UITableViewCell对象中 表视图可以分区显⽰示数据,每个分区 ...
- UI一揽子计划 1 (UIView UILabel)
第一天 1.UIView ·建立一个空模板后,首先将ARC模式关闭,因为iOS采用MRC模式,即手动管理内存. ·重写dealloc方法. - (void)dealloc { [_window rel ...
- UI一揽子计划 2 (UITextField UIButton)
UItextField 一.创建一个UITextField时候默认就弹出键盘 1 对齐方式 textField.textAlignment = NSTextAlignmentCente ...
- UI一揽子计划 24 (MVC、通知、)
一.MVC Model - View - Controller 即 模型 - 视图 - 控制器 Modle (模型) 存储 处理数据 为应用程序提供数据. View (视图)展示用户界面视图,提供 ...
- UI一揽子计划 7 ( UIScrollView、 UIPageControl、综合所学简单相册)
---- 创建一个UIScrollView UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:[UIScreen ma ...
- UI一揽子计划 8 (UINavigationController 、界面通信 、NSTimer 、NSUserDefaults)
一.UINavigationController // 创建一个导航控制器 // 创建一个控制器作为根控制器 去管理 RootViewController *rootVC ...
- UI一揽子计划 14 (拖控件 、)
- (void)viewDidLoad { [super viewDidLoad]; // 注册cell // 如果使用Xib 文件加载cell 的话 一定要注 ...
最新文章
- 别再写满屏的try-catch了,真丑,全局异常处理不会吗?
- python打印表格_使用 Python 打印漂亮的表格,这两项基本功你可会?
- linux 设置固定网络转发_linux服务器配置双网卡转发和静态路由及默认网关
- 让后台服务不被杀———电话录音
- JDK1.8 JVM元数据区取代了永久代,元数据空间并不在虚拟机中,而是使用本地内存
- 计算机画画作品 人物 游泳,夏天主题的儿童画-游泳的儿童绘画作品
- JavaScript得到鼠标指针相对于浏览器页面(或客户区)的水平坐标(兼容ie,ff)...
- 【嵌入式Linux】嵌入式Linux应用开发基础知识之文件IO
- tftp的安装、设置以及put、get传输实验
- JavaScript年月日和时间戳互转
- 最大表示法--环形字符串最大字典序(HDU 5442)
- 在ASP.NET 2.0中直接得到本页面生成的HTML代码(转自孟宪会之精彩世界)
- python:使用sklearn 计算 precision、recall、F1 score(多分类)
- Linux安装MySQL安装包下载
- 谷歌浏览器截取长屏幕(全屏截图)
- 云计算系统典型物理架构
- TINA仿真系列之555定时器
- 单元测试利器——手把手教你使用Mockito
- linux服务器实训心得体会,linux服务器实训心得体会
- 敢不敢挑战,半年学好英语。
热门文章
- java 获取当前方法的调用栈
- iOS微信/支付宝/苹果内付支付流程图1
- iview中table排序
- JavaBean,List,Map转成json格式
- java错误:The superclass “javax.servlet.http.HttpServlet“ was not found on the Java Bu
- Matlab 振动加速度 对应的振动频率,由振动加速度进行频域二次积分求振动位移...
- 10个问题让你秒懂变压器!
- ISP和DSP的区别
- 【100天精通python】Day1:python入门_初识python,搭建python环境,运行第一个python小程序
- 计算机三级信息安全技术易错、不好记的选择、填空内容