iOS中控制屏幕旋轉相關方法
shouldAutorotate:是否支持屏幕旋轉
{
//返回值為YES支持屏幕旋轉,返回值為NO不支持屏幕旋轉,默認值為YES。
return YES;
}```
supportedInterfaceOrientations:設置當前界面所支持的屏幕方向
```- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
//支持所有的屏幕方向
return UIInterfaceOrientationMaskAll;
//支持向左或向右
//return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}```
layoutSubviews:重寫父類的layoutSubviews,當屏幕旋轉時會執行此方法,一般在此方法中對當前的子視圖重新布局
```- (void)layoutSubviews
{
[super layoutSubviews];//調用父類的方法
//獲取當前屏幕的方向狀態, [UIApplication sharedApplication]獲得當前的應用程序
NSInteger orientation = [UIApplication sharedApplication].statusBarOrientation;
switch (orientation) {
//屏幕左轉時的重新布局
case UIInterfaceOrientationLandscapeLeft:
{
......
}
break;
//屏幕右轉時的重新布局
case UIInterfaceOrientationLandscapeRight:
{
......
}
break;
//屏幕轉回來后的重新布局
case UIInterfaceOrientationPortrait:
{
......
}
break;
default:
break;
}
}```
#警示框
iOS8.0之前
警示框:
```//創建一個警示框并初始化
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"標題" message:@"內容" delegate:self cancelButtonTitle:nil otherButtonTitles:@"確定", @"再次確定", nil];
//顯示alertView
[alertView show];
alertView:clickedButtonAtIndex:警示框的代理方法
{
NSLog(@"buttonIndex = %ld",buttonIndex);//輸出點擊按鈕的標記index,根據添加順序從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);//輸出點擊按鈕的標記index,根據添加順序從0開始一次增加
}```
iOS8.0之后
UIAlertController:提示框控制器 這是iOS8之后出得新類,用于替換原來的UIAlertView 和 UIActionSheet
* title:提示框按鈕的標題
* style:按鈕的樣式
* handler:點擊該按鈕就會執行的語句(block)
//初始化一個UIAlertController
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示框" message:@"內容" preferredStyle:UIAlertControllerStyleAlert];
//為提示框添加點擊按鈕alertAction
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"點擊了取消按鈕");
}];
//將創建好的action添加到提示框視圖控制器上
[alertController addAction:cancelAction];
//再添加一個確定按鈕
UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"點擊了確定按鈕");
}];
[alertController addAction:sureAction];```