如何将具有横向边界的子视图添加到纵向视图

时间:2019-05-21 18:57:12

标签: ios

enter image description here我有ParentViewController回来了

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

我用

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(onDeviceOrientationDidChange:)
                                             name:UIDeviceOrientationDidChangeNotification
                                           object:nil];

检测方向变化。 onDeviceOrientationDidChange我尝试添加带有景观边界的子视图。但是,当我尝试向景观视图添加子视图时,该子视图会显示在右上方。但是我想要它在左上方。请查看屏幕截图中的红色框。如何将红色框移到左上方?

蓝框为横向视图。

- (void)onDeviceOrientationDidChange:(NSNotification *)notification
{
    CGRect fr = self.view.frame;
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    if (UIDeviceOrientationIsLandscape(deviceOrientation)) {
        UIView *landscapeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, fr.size.height, fr.size.width)];
        [landscapeView setFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
        landscapeView.backgroundColor = kBlueColor;
        [self.view addSubview:landscapeView];
        UIView *landscapeSubView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
        landscapeSubView.backgroundColor = kRedColor;
        [landscapeView addSubview:landscapeSubView];        
    }
}

0 个答案:

没有答案