iOS 屏幕横屏的实现

在viewDidLoad里写上

self.view.transform = CGAffineTransformMakeRotation(M_PI/2);

然后要注意的是横屏之后要设置界面的各个控件自动适应约束autoresizingMask

UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight//这几种如何约束请参照interfacebuilder

下面是布局代码

  _functionView    = [[UIView alloc] initWithFrame:CGRectMake(0,self.view.frame.size.height - 50,self.view.frame.size.width,50)];
    _functionView.backgroundColor = [UIColor clearColor];
    _functionView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
    [self.view addSubview:_functionView];
    
    _closeButton = [[UIButton alloc]initWithFrame:CGRectMake(16, 16, 44, 44)];
    [_closeButton addTarget:self action:@selector(doneDidTouch:) forControlEvents:UIControlEventTouchUpInside];
    [_closeButton setImage:[UIImage imageNamed:@"[email protected]"] forState:UIControlStateNormal];
    _closeButton.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
    [self.view addSubview:_closeButton];

    _progressLabel = [[UILabel alloc] initWithFrame:CGRectMake(46, 1, 50, topH)];
    _progressLabel.textColor = [UIColor whiteColor];
    _progressLabel.opaque = NO;
    _progressLabel.adjustsFontSizeToFitWidth = NO;
    _progressLabel.textAlignment = NSTextAlignmentRight;
    _progressLabel.text = @"";
    _progressLabel.font = [UIFont systemFontOfSize:12];
    
    _progressSlider = [[UISlider alloc] initWithFrame:CGRectMake(100, 2, width-197, topH)];
    _progressSlider.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    _progressSlider.continuous = NO;
    _progressSlider.value = 0;

    _leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(width-92, 1, 60, topH)];
    _leftLabel.textColor = [UIColor whiteColor];
    _leftLabel.opaque = NO;
    _leftLabel.adjustsFontSizeToFitWidth = NO;
    _leftLabel.textAlignment = NSTextAlignmentLeft;
    _leftLabel.text = @"";
    _leftLabel.font = [UIFont systemFontOfSize:12];
    _leftLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
    
    _playButton = [[UIButton alloc]initWithFrame:CGRectMake(16, 0, 44, 44)];
    [_playButton setImage:@"playback_pause.png" forState:UIControlStateNormal];
    [_playButton addTarget:self action:@selector(playDidTouch:) forControlEvents:UIControlEventTouchUpInside];
    
    _bottomShadow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bottom_shadow.png"]];
    _bottomShadow.frame = CGRectMake(0, 0, _functionView.bounds.size.width, _functionView.bounds.size.height);
    _bottomShadow.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    
    [_functionView addSubview:_bottomShadow];
    [_functionView addSubview:_playButton];
    [_functionView addSubview:_progressLabel];
    [_functionView addSubview:_progressSlider];
    [_functionView addSubview:_leftLabel];

你可能感兴趣的:(iOS 屏幕横屏的实现)