ios - 将 UIImage 定位在 UIScrollView 的中心进行缩放

标签 ios objective-c uiscrollview uiimageview

我正在尝试使用 UIScrollview 和 UIImageview 来缩放图像...UIscrollview 是 300 x 300 并位于 Storyboard 的中心..但是它有效...当 View 加载时图像位于左上角(不完全是) ScrollView ..但我希望它在 View 加载时位于 ScrollView 的中心......任何人都可以提出任何建议吗??

#import "ViewController.h"
@interface ViewController ()<UIScrollViewDelegate>

@property (strong, nonatomic) IBOutlet UIScrollView *scrollview;
@property (strong,nonatomic) UIImageView *imageview;

@end

@implementation ViewController
@synthesize scrollview,imageview;


-(void)viewDidLoad {
[super viewDidLoad];


scrollview.delegate=self;

UIImage *image = [UIImage imageNamed:@"back.jpg"];
imageview = [[UIImageView alloc] initWithImage:image];

imageview.contentMode=UIViewContentModeCenter;
imageview.frame=CGRectMake(0,0,scrollview.frame.size.width,scrollview.frame.size.height);
imageview.userInteractionEnabled=YES;
[scrollview addSubview:imageview];
scrollview.contentSize=image.size;

}

-(void)viewWillAppear:(BOOL)animated{


CGRect scrollviewframe=scrollview.frame;
CGFloat scaleWidth=scrollviewframe.size.width/scrollview.contentSize.width;
CGFloat scaleHeight=scrollviewframe.size.height/scrollview.contentSize.height;
CGFloat scaleMin= MIN(scaleWidth, scaleHeight);
scrollview.minimumZoomScale=scaleMin;
scrollview.maximumZoomScale=1;
scrollview.zoomScale=scaleMin;
[self centerScrollviewContents];

}

最佳答案

我建议在您的 Storyboard 中使用自动布局。将 UIImageViewUIScrollView 之间的前导空间、顶部空间、尾部空间和底部空间设置为 0(或者在填充的情况下为正整数)应该可以解决问题。

关于ios - 将 UIImage 定位在 UIScrollView 的中心进行缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32290377/

相关文章:

ios - RFQuiltLayout 和 UICollectionView

iphone - 将值从 UIView 传递到 UIViewController

ios - iPhone 和 iPad 上的 View 放置方式不同

javascript - 如何在 Angular 中设置靠近垫行的滚动位置?

ios - Xamarin.IOS 从 CNContactViewController.FromContact 返回到 Application

ios - 如何使用索引(:) with [SomeProtocol]?

ios - UIAlertView 当没有按钮按下时计数 10 秒并执行某些操作

iphone - 如何将白色像素更改为黑色像素

ios - 如何使用 Swift 更新 CoreData 数据

ios - 仅在公司员工的手机上部署 iPhone 应用程序?