有没有办法在Interface Builder / storyboard中自动调整UIImageView的大小?

时间:2012-08-15 09:21:34

标签: objective-c xcode uiimageview

在故事板或界面构建器中为uiimageview设置图像时,它会保留原始UIImageView的形状,这意味着图像本身完全失真。

要使UIImageView的大小与其图像相匹配,我必须手动输入文件检查器的大小。

UIImageView是否可以自动检测其图像的原始大小并相应地调整其自身大小?

4 个答案:

答案 0 :(得分:49)

我假设你指的是故事板/ IB中的布局。要获得图像的原始大小,只需选择图像并按键盘上的命令 + = 。要按比例重新调整大小,请选择角落并在重新调整大小时按住Shift键。

答案 1 :(得分:3)

为什么不在代码中设置uiimageview的大小? 这是你需要做的......

//get the Image
UIImage *img = [UIImage imageNamed:@"img.png"];

CGFloat x = img.origin.x;
CGFloat y = img.origin.y;
CGFloat width = img.size.width;
CGFloat height = img.size.height;
imageView.frame = CGRectMake(x, y, width, height);  //imageView is your uiimageview's reference

答案 2 :(得分:1)

使用contentMode

 imageView.contentMode = UIViewContentModeScaleAspectFit;

修改: 我刚看到你似乎想要调整imageViews大小到图像....不同然后

答案 3 :(得分:-3)

这就是我所做的:

imageView.clipsToBounds = YES;
[imageView setContentMode: UIViewContentModeScaleAspectFit];

这对我有用。