iPad 旋转到横向时调整 UIImageView 的尺寸

Adjusting dimensions of UIImageView when iPad is rotated to Landscape orientation

我有一个简单的应用程序,当在 UITableViewController 中单击一个单元格时,它会显示 UIPageViewControllerUIPageViewController(称为 Leaflets)在 iPhone 和 iPad 中以非常好的布局显示了 6 个图像:

- (void)imageDimensions
{
    [self.view setBackgroundColor:[UIColor clearColor]];
    CGRect insetFrame;
    if (IDIOM == IPAD)
    {
        NSLog(@"iPad");
        insetFrame = CGRectMake(160, 70, self.view.frame.size.width-320, self.view.frame.size.height-115);

    }
    else
    {
        NSLog(@"iPhone");
        insetFrame = CGRectMake(30, 20, self.view.frame.size.width-60, self.view.frame.size.height-15);

    }

    _imageView = [[UIImageView alloc] initWithFrame:insetFrame];
    [_imageView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
    [_imageView setImage:[UIImage imageNamed:_model.imageName]];
    [[self view] addSubview:_imageView];

}

当我将 iPad 旋转到 Landscape 模式时,理想情况下,我希望 insetFrame 发生变化,这样图像就不会在屏幕上伸展开来。

我尝试在 viewDidLoad 中放置一个 NSNotification:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceRotated:) name:UIDeviceOrientationDidChangeNotification object:nil];

调用:

-(void)deviceRotated:(NSNotification*)notification
{
    NSLog(@"Rotation");
    CGRect insetFrame;
    insetFrame = CGRectMake(160, 70, self.view.frame.size.width-620, self.view.frame.size.height-715);
    _imageView = [[UIImageView alloc] initWithFrame:insetFrame];
    [_imageView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
    [_imageView setImage:[UIImage imageNamed:_model.imageName]];
    [[self view] addSubview:_imageView];


}

肯定会调用这里的方法,但有两个问题。

问题

在不将 imageView 添加到视图的情况下,属性 上的图像没有任何变化,如果我保留上面显示的方法,它会将图像添加到现有图像的顶部.

理想情况下,我想从视图中删除图像并重置它的纵向模式坐标。

[_imageView removeFromSuperview]; 

如果我在 deviceRotated 方法中设置它,每次旋转视图时都会调用它,所以它总是会被移除,等等。

我想要一种说法,如果是风景,则显示此框架;如果是肖像,则显示此框架。

如有任何相关指导,我们将不胜感激。

您是否尝试过为重绘和重新布局设置显示?

CGRect insetFrame;
insetFrame = CGRectMake(160, 70, self.view.frame.size.width-620, self.view.frame.size.height-715);
_imageView = [[UIImageView alloc] initWithFrame:insetFrame];
[_imageView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[_imageView setImage:[UIImage imageNamed:_model.imageName]];
[_imageView setNeedsDisplay];
[_imageView setNeedsLayout];

这可能对你有用。确定删除 [[self view] addSubview:_imageView] 行;您将其重复添加到显示中。