在布局视图中
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 200 , 200)];
self.imageView.backgroundColor = [UIColor redColor];
[self addSubview:self.imageView];
}
return self;
}
- (void)layoutSubviews {
// 获取到屏幕的方向
在这个方法中 屏幕旋转等会重新执行这个方法
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationLandscapeLeft) {
self.imageView.frame = CGRectMake(200, 200, 20, 20);
self.imageView.backgroundColor = [UIColor blackColor];
}
if (orientation == UIInterfaceOrientationLandscapeRight) {
self.imageView.frame = CGRectMake(0, 200, 20, 20);
self.imageView.backgroundColor = [UIColor blueColor];
}
}
在视图控制器中重写
//屏幕支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;//如果这里支持的方法固定,那么只会显示支持的那个方法下的设置。
}