iOS中控制屏幕旋轉(zhuǎn)相關(guān)方法
shouldAutorotate:是否支持屏幕旋轉(zhuǎn)
{
//返回值為YES支持屏幕旋轉(zhuǎn)傻铣,返回值為NO不支持屏幕旋轉(zhuǎn)裆馒,默認(rèn)值為YES秉溉。
return YES;
}```
supportedInterfaceOrientations:設(shè)置當(dāng)前界面所支持的屏幕方向
```- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
//支持所有的屏幕方向
return UIInterfaceOrientationMaskAll;
//支持向左或向右
//return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}```
layoutSubviews:重寫父類的layoutSubviews滞磺,當(dāng)屏幕旋轉(zhuǎn)時會執(zhí)行此方法国旷,一般在此方法中對當(dāng)前的子視圖重新布局
```- (void)layoutSubviews
{
[super layoutSubviews];//調(diào)用父類的方法
//獲取當(dāng)前屏幕的方向狀態(tài)柏蘑, [UIApplication sharedApplication]獲得當(dāng)前的應(yīng)用程序
NSInteger orientation = [UIApplication sharedApplication].statusBarOrientation;
switch (orientation) {
//屏幕左轉(zhuǎn)時的重新布局
case UIInterfaceOrientationLandscapeLeft:
{
......
}
break卸留;
//屏幕右轉(zhuǎn)時的重新布局
case UIInterfaceOrientationLandscapeRight:
{
......
}
break;
//屏幕轉(zhuǎn)回來后的重新布局
case UIInterfaceOrientationPortrait:
{
......
}
break;
default:
break;
}
}```
#警示框
iOS8.0之前
警示框:
```//創(chuàng)建一個警示框并初始化
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"標(biāo)題" message:@"內(nèi)容" delegate:self cancelButtonTitle:nil otherButtonTitles:@"確定", @"再次確定", nil];
//顯示alertView
[alertView show];
alertView:clickedButtonAtIndex:警示框的代理方法
{
NSLog(@"buttonIndex = %ld",buttonIndex);//輸出點擊按鈕的標(biāo)記index堤瘤,根據(jù)添加順序從0開始一次增加
}```
屏幕下方彈框:
```//初始化sheet
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"sheet" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"確定" otherButtonTitles:@"其余", nil];
//顯示
[sheet showInView:sheet];```
actionSheet:clickedButtonAtIndex:屏幕下方彈框的代理方法
```- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"buttonIndex = %ld",buttonIndex);//輸出點擊按鈕的標(biāo)記index,根據(jù)添加順序從0開始一次增加
}```
iOS8.0之后
UIAlertController:提示框控制器 這是iOS8之后出得新類慎王,用于替換原來的UIAlertView 和 UIActionSheet
* title:提示框按鈕的標(biāo)題
* style:按鈕的樣式
* handler:點擊該按鈕就會執(zhí)行的語句(block)
//初始化一個UIAlertController
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示框" message:@"內(nèi)容" preferredStyle:UIAlertControllerStyleAlert];
//為提示框添加點擊按鈕alertAction
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"點擊了取消按鈕");
}];
//將創(chuàng)建好的action添加到提示框視圖控制器上
[alertController addAction:cancelAction];
//再添加一個確定按鈕
UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"點擊了確定按鈕");
}];
[alertController addAction:sureAction];```