iOS-AutoLayout布局屏幕旋转(全屏播放)

代码如下(需要用到Masonry第三方布局库)

#import "MainVC.h"

@interface MainVC ()

@property (nonatomic,strong) UIView *playView;

@property (nonatomic,strong) UIButton *fullBtn;

@end

@implementation MainVC


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
  
    _playView = [[UIView alloc]init];
    _playView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_playView];
    [_playView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(0);
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        make.height.mas_equalTo(200);
    }];
    
    UILabel *title = [[UILabel alloc]init];
    title.text = @"视频标题";
    [_playView addSubview:title];
    [title mas_updateConstraints:^(MASConstraintMaker *make) {
      
        make.centerX.equalTo(_playView.mas_centerX);
        make.centerY.equalTo(_playView.mas_centerY);
        
    }];

    
    UILabel *time = [[UILabel alloc]init];
    time.text = @"08:30/30:00";
    [_playView addSubview:time];
    [time mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_playView).offset(10);
        make.bottom.equalTo(_playView).offset(0);
    }];
    
    _fullBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_fullBtn setTitle:@"大" forState:UIControlStateNormal];
    [_fullBtn setTitle:@"小" forState:UIControlStateSelected];
    [_fullBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    [_fullBtn addTarget:self action:@selector(changeDevice) forControlEvents:UIControlEventTouchUpInside];
    [_playView addSubview:_fullBtn];
    [_fullBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(_playView).offset(-10);
        make.bottom.equalTo(_playView).offset(0);
    }];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceChange) name:UIDeviceOrientationDidChangeNotification object:nil];
}
- (void)changeDevice{
    
    _fullBtn.selected = !_fullBtn.selected;
    if (_fullBtn.selected) {
         [[UIDevice currentDevice] setValue:@(UIDeviceOrientationLandscapeLeft) forKey:@"orientation"];

    }else{
         [[UIDevice currentDevice] setValue:@(UIDeviceOrientationPortrait) forKey:@"orientation"];
    }
    
}
- (void)deviceChange{
    
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;

    switch (deviceOrientation) {
            
        case UIDeviceOrientationPortrait:
        {
            _fullBtn.selected  = NO;
            [_playView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(200);
            }];
        }
            break;
        case UIDeviceOrientationLandscapeLeft:
        {
            _fullBtn.selected  = YES;

            [_playView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(self.view.bounds.size.height);
            }];
        }
            break;
        case UIDeviceOrientationLandscapeRight:
        {
            _fullBtn.selected  = YES;

            [_playView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(self.view.bounds.size.height);
            }];
        }
            break;
            
        default:
            break;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 177,218评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,810评论 4 61
  • 彩虹瓢 文/琴簿白 彩虹的另一头是妈妈住的地方。——题记 女孩琴从小就住在外婆家,在她很小很小的时候,当别的小朋友...
    一只不倒猫阅读 1,733评论 0 0
  • 1 有关孩子假期培训,一说为了提高孩子学业筹码,一说满足孩子兴趣需求,暑假一到各大培训机构趋之若鹜,各类优惠广告、...
    江村塘影阅读 1,657评论 0 0
  • 真正想要改变的人,就算是没有别人的督促,亦会学会自律。 越来越多介绍如何自律的书和讲座出现在我们的生活里。全部都是...
    scmsuki阅读 1,774评论 0 0

友情链接更多精彩内容