一、UIView自定义视图
因为在实际开发过程中,可能由于繁琐的创建相同的Label、textfield和Button组合,为了实现编程工作的高效率,我们可以自定义视图,并且将它们封装起来。
·
自定义视图步骤
1.
创建一个
UIView
的子类
2. 把想要的视图 在子类中创建并添加(在初始化方法中添加)
2. 把想要的视图 在子类中创建并添加(在初始化方法中添加)
3.在需要的地方初始化子类的到想要的布局
《《《《《《《《《头文件中》》》》》》》》》
@interface
LTView :
UIView
// 创建属性 开放接口 方便外界访问
@property ( nonatomic , retain ) UILabel *label;
@property ( nonatomic , retain ) UITextField *textField;
// 创建属性 开放接口 方便外界访问
@property ( nonatomic , retain ) UILabel *label;
@property ( nonatomic , retain ) UITextField *textField;
@end
《《《《《《《《《.m文件中》》》》》》》》》》》》》
- (
instancetype
)initWithFrame:(
CGRect
)frame
{
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 ];
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 ];
[_textField
release
];
}
return
self
;
}
二、 屏幕旋转
//
布局子视图
重写父类的方法
// 如果已经更改了 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 );
// 如果已经更改了 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}
*/
* 屏幕旋转的步骤
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}
*/
}
三、UIViewControl
*
视图控制器
自带了一个View (跟屏幕等大) 自带View的属性
·
加载视图
加载的就是
self.view
·
重写父类的方法
- (
void
)loadView
{
// 如果使用以下的 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 )的方法中添加。
*/
{
// 如果使用以下的 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 )的方法中添加。
*/
}
·
视图加载完毕
- (
void
)viewDidLoad {
self.view.backgroundColor = [UIColor redColor];
//
视图加载完毕了
self.view
// [super viewDidLoad];
/**
* 把 登陆的 View 取出来
*/
LTBView *loginView = ( LTBView *) self . view ;
[loginView. loginButton addTarget : self action : @selector (actionLoginButtton:) forControlEvents :( UIControlEventTouchUpInside )];
// [super viewDidLoad];
/**
* 把 登陆的 View 取出来
*/
LTBView *loginView = ( LTBView *) self . view ;
[loginView. loginButton addTarget : self action : @selector (actionLoginButtton:) forControlEvents :( UIControlEventTouchUpInside )];
loginView.passwordView.textField.delegate = self;
}
- (
void
)viewWillAppear:(
BOOL
)animated
{
NSLog ( @" 将要显示 " );
}
- ( void )viewWillDisappear:( BOOL )animated
{
NSLog ( @" 将要消失 " );
}
- ( void )viewDidAppear:( BOOL )animated
{
NSLog ( @" 已经显示 " );
}
- ( void )viewDidDisappear:( BOOL )animated
{
NSLog ( @" 已经消失 " );
{
NSLog ( @" 将要显示 " );
}
- ( void )viewWillDisappear:( BOOL )animated
{
NSLog ( @" 将要消失 " );
}
- ( void )viewDidAppear:( BOOL )animated
{
NSLog ( @" 已经显示 " );
}
- ( void )viewDidDisappear:( BOOL )animated
{
NSLog ( @" 已经消失 " );
·
内存警告时触发的。
- (
void
)didReceiveMemoryWarning {
// 把视图销毁。
// self.view = nil;
if ([ self isViewLoaded ] == YES && self . view . window == nil ) { // 如果视图已经被加载过且视图不是当前在显示的 那么我们才能把视图销毁
// 把不用的视图销毁了
self . view = nil ;
// 把视图销毁。
// self.view = nil;
if ([ self isViewLoaded ] == YES && self . view . window == nil ) { // 如果视图已经被加载过且视图不是当前在显示的 那么我们才能把视图销毁
// 把不用的视图销毁了
self . view = nil ;
}
}
·用UIViewControl 实现页面之间的跳转
- (
void
)viewDidLoad {
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 )];
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 )];
[self.view addSubview:button1];
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- ( void )didReceiveMemoryWarning {
[ super didReceiveMemoryWarning ];
// Dispose of any resources that can be recreated.
}
- ( void )action:( UIButton *)button
{
}
- ( void )didReceiveMemoryWarning {
[ super didReceiveMemoryWarning ];
// Dispose of any resources that can be recreated.
}
- ( void )action:( UIButton *)button
{
·返回上一层试图控制器
[self dismissViewControllerAnimated:YES completion:nil];
}
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<----------------------------------------
· 夏炎分享的, 上弹窗。 showsheet
1.UIACtionsheet
2.设计样式 主题 self 取消 确定 第一个按钮 第二个按钮 从下往上0-N actionsheet。actionsheetstyle = (枚举类型的样式)。 actionsheet showInView self.windows
3.代理方法 if (buttonIndex == 0){ [self showAlert :@"确定"]} else if () {} 再写一个UIAlertView 方法
2.设计样式 主题 self 取消 确定 第一个按钮 第二个按钮 从下往上0-N actionsheet。actionsheetstyle = (枚举类型的样式)。 actionsheet showInView self.windows
3.代理方法 if (buttonIndex == 0){ [self showAlert :@"确定"]} else if () {} 再写一个UIAlertView 方法