xcode 有没有办法在界面生成器/故事板中自动调整 UIImageView 的大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11966838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:14:19  来源:igfitidea点击:

Is there a way to auto-size a UIImageView in Interface Builder/storyboard?

objective-cxcodeuiimageview

提问by zakdances

When you set a image for a uiimageview in storyboard or interface builder, it retains the shape of the original UIImageView, which means that the image itself is completely distorted.

当您在 storyboard 或界面构建器中为 uiimageview 设置图像时,它保留了原始 的形状UIImageView,这意味着图像本身已完全扭曲。

To match the size of the UIImageViewto its image, I have to manually enter in the size from the file inspector.

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

Is there a way that UIImageViewcan automatically detect the native size of its image and size its self accordingly?

有没有一种方法UIImageView可以自动检测其图像的原始大小并相应地调整其自身的大小?

回答by Nelson Brian

I am assuming you are referring to layout in storyboard/IB. To get the native size of the image just select the image and press Command+ =on the keyboard. the to re-size it proportionally select the corner and hold down the shift key when you re-size it.

我假设您指的是情节提要/IB 中的布局。要获得图像的原始大小,只需选择图像并按键盘上的Command+ =。按比例重新调整大小 选择角并在重新调整大小时按住 shift 键。

回答by Humayun

Why don't you set the size of your uiimageview in code? Here's what you need to do...

为什么不在代码中设置 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

回答by Mario

Use the contentMode

使用内容模式

?imageView.contentMode = UIViewContentModeScaleAspectFit;

Edit: I just saw you seem to want to adjust imageViews size to the image.... Different then

编辑:我刚刚看到你似乎想将 imageViews 大小调整为图像......然后不同

回答by Rafael

This is what I did:

这就是我所做的:

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

That is working for me.

这对我有用。