iphone - UIImageView 旋转不正确?

标签 iphone objective-c ios uiimageview

我在 UIViewController 中有一个 UIScrollView。它启用了滚动和缩放。它包含一个 UIImageView,当用户旋转设备时,图像保持居中。问题是,当设备旋转时,它实际上出现在左侧而不是中心,它应该停留在那里。这是我正在使用的代码。当 UIScrollView 旋转时调用设置框架:

-(void)setFrame:(CGRect)frame {

    float previousWidth = self.frame.size.width;
    float newWidth = frame.size.width;

    [super setFrame:frame];
    self.imageView.center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);

    [self setupScales];

    self.zoomScale = self.zoomScale * (newWidth / previousWidth);
}

最佳答案

我在我的 uiscrollview 子类中使用以下 layoutsubviews 方法:

- (void)layoutSubviews {
    [super layoutSubviews];

    if ([[self subviews] count] == 0) {
        return;
    }

    // center the image as it becomes smaller than the size of the screen
    CGSize boundsSize = self.bounds.size;
    CGRect frameToCenter = ((UIView*)[[self subviews] objectAtIndex:0]).frame;

    // center horizontally
    if (frameToCenter.size.width < boundsSize.width)
        frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
    else
        frameToCenter.origin.x = 0;

    // center vertically
    if (frameToCenter.size.height < boundsSize.height)
        frameToCenter.origin.y = ((boundsSize.height - frameToCenter.size.height) / 2);
    else
        frameToCenter.origin.y = 0;

    ((UIView*)[[self subviews] objectAtIndex:0]).frame = frameToCenter;
}

关于iphone - UIImageView 旋转不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10504300/

相关文章:

iphone - 在哪里可以找到 Apple CrashLanding 示例应用程序?

iphone - appFailedToResumeInTime 崩溃

ios - 将KVO添加到核心数据的类别属性

objective-c - 访问 NSDictionary 时的 SIGABRT

iphone - 在行选择上更改 UITableView accessoryView

ios - 如何找到 Xcode 项目的圈复杂度?

ios - 仅当场景数据改变时重绘场景

ios - 在单个 UIVIewController 中垂直放置两个全屏 UIView?

iphone - 在 IOS 上禁用 ShareKit 上的自动消息框

ios - 如何取消选择选定的 UITableView 单元格?