Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

楼主你是如何兼容IOS7下横屏的,貌似会显示旋转逆时针90度的。你是怎么解决这个问题的? #2

Open
jaccty opened this issue Jul 20, 2016 · 1 comment

Comments

@jaccty
Copy link

jaccty commented Jul 20, 2016

楼主你是如何兼容IOS7下横屏的,貌似会显示旋转逆时针90度的。你是怎么解决这个问题的?核心代码是?

@yizzuide
Copy link
Owner

yizzuide commented Jul 20, 2016

有两种解决方案:

第一种,使用侦听通知,在顶级视图添加代码

  // 侦听屏幕旋转
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willRotateWithNoti:) name:UIDeviceOrientationDidChangeNotification object:nil];

- (void)willRotateWithNoti:(NSNotification *)noti {
    [UIView animateWithDuration:0.25 animations:^{
        self.frame = CGRectMake(0, 0, ScreenSize.width, ScreenSize.height);
    }];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    // 重新布局对话框视图
    self.dialogView.centerX = self.centerX;
    self.dialogView.centerY = self.centerY;
    [self.dialogView layoutIfNeeded];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

第二种方式,AutoLayout(简单,推荐使用),同样在顶级视图添加代码

self.translatesAutoresizingMaskIntoConstraints = NO;
    NSArray *hConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[maskView]-0-|" options:0 metrics:nil views:@{@"maskView":self}];
    [self.frontWindow addConstraints:hConstraint];
    NSArray *vConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[maskView]-0-|" options:0 metrics:nil views:@{@"maskView":self}];
    [self.frontWindow addConstraints:vConstraint];

// 然后再布局,这是必写的
- (void)layoutSubviews
{
    [super layoutSubviews];
    // 重新布局对话框视图
    self.dialogView.centerX = self.centerX;
    self.dialogView.centerY = self.centerY;
    [self.dialogView layoutIfNeeded];
}

可以参考本项目的XFMaskView类,如有对你帮助,可以Star

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants