ios - 相应地缩放 UIButton 中的图像

标签 ios objective-c uibutton

我有一个 UIButton,我使用 setImage: 方法添加了一个图像 图像小于按钮的边界,因此按预期居中。

现在,如果我调整按钮的大小,我希望图像也缩小。 (目前,如果我调整按钮的大小,我的图像会保持相同的大小,因为我调整大小后的按钮的边界仍然比图像大)。

关于如何实现这一点有什么想法吗?

最佳答案

只需设置 UIButton 内的 ImageView 的 ContentMode。

[[self.button imageView] setContentMode: UIViewContentModeScaleAspectFit];
[self.button setImage:[UIImage imageNamed:stretchImage] forState:UIControlStateNormal];

希望这对您有所帮助。

关于ios - 相应地缩放 UIButton 中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21894062/

相关文章:

objective-c - 以 UTF16 格式写入时 iOS 导出文件中的额外空格

ios - 在 UITextview 中定位 UIButton

ios - 拉伸(stretch) UIButton 的背景图像

ios - 过度使用 App Delegate?

ios - swift 2 : screen rotation only on full screen video

javascript - 如何防止在 PhoneGap 中双击移动设备滚动?

iphone - 逐帧播放视频

ios - UIImageView 正在显示放大的图像

objective-c - cocos2d 改变 Sprite 纹理

ios - 点击时 UIButton 未获取事件