这是我第一次设计一个iOS应用程序,所以我想确保我正确地理解了这个行为。
我为Photoshop中的导航栏设计了一个自定义的条形按钮图标。我在Photoshop中保存的最后一张图片是102x45,是的,我意识到这些尺寸比iOS 7设计指南中推荐的44x44要大。
无论如何,我将图像放在资产文件夹中,然后以编程方式使用以下代码设置bar按钮项:
UIImage* firstButtonImage = [UIImage imageNamed:@"loginbutton1"];
CGRect frame = CGRectMake(0, 0, 102, 45);
UIButton * someButton = [[UIButton alloc] initWithFrame:frame];
[someButton setBackgroundImage:firstButtonImage forState:UIControlStateNormal];
[someButton addTarget:self action:@selector(didTapLoginButton:)
forControlEvents:UIControlEventTouchUpInside];
self.rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:someButton];
self.navItem.rightBarButtonItem = self.rightBarButton;如您所见,我将帧的宽度和高度设置为图像的确切大小。当我第一次运行这个应用程序时,我不喜欢这张图片,觉得它太大了。因此,我在这个语句中更改了宽度和高度参数:
CGRect frame = CGRectMake(0, 0, 70, 30);现在,图像在iPhone屏幕上看起来很完美。这是在iPhone 4s上。
所以我的主要问题是,当我改变帧的大小时,到底发生了什么?由于现在的帧小于实际的图像大小,那么图像是否会自动缩小以适应帧内的需要?
发布于 2014-04-09 22:21:53
是的,因为您使用的是backgroundImage (而不是Image),所以图像会被缩放。这两种图像都有不同的行为。
检查Xcode接口生成器,您可以在那里看到,您可以设置两个图像:图像和背景。背景是对整个UIImage框架进行缩放的UIButton。

UIButton类引用允许您访问image的imageView (而不是backgroundImage的imageView)。
因为您可以访问imageView,所以可以用以下方式更改图像的模式:
[[someButton imageView] setContentMode:UIViewContentModeBottomLeft];在UIView类引用中,您可以检查苹果提供的所有UIViewContentModes。
您可以检查是否更改了代码:
[someButton setImage:firstButtonImage forState:UIControlStateNormal];
[[someButton imageView] setContentMode:UIViewContentModeBottomRight];https://stackoverflow.com/questions/22973573
复制相似问题