如何调整UIButton的图像大小?我正在设置这样的图像:
[myLikesButton setImage:[UIImage imageNamed:@"icon-heart.png"] forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
但是,这会将图像填满整个按钮,如何使图像变小?
我使用自动布局.以下是视图的初始状态.
中间是视图中包含的按钮.该按钮具有contentMode Aspect Fill,并且图像被设置为按钮的背景图像.

然后我使用以下代码转换视图,这将放大中心卡以填充屏幕,并将图像移动到视图的顶部:
cardTrailingSpaceConstraint.constant = 0
cardLeadingSpaceConstraint.constant = 0
cardView.removeConstraint(cardAspectRatioConstraint)
let cardHeightConstraint = NSLayoutConstraint(item: cardView, attribute: .Height, relatedBy: .Equal, toItem: view, attribute: .Height, multiplier: 1.0, constant: 0)
view.addConstraint(cardHeightConstraint)
dishImageButton.removeConstraint(dishButtonBottomSpaceConstraint)
let dishButtonHeightConstraint = NSLayoutConstraint(item: dishImageButton, attribute: .Height, relatedBy: .Equal, toItem: cardView, attribute: .Height, multiplier: 0.2, constant: 0)
cardView.addConstraint(dishButtonHeightConstraint)
cardView.setNeedsUpdateConstraints()
UIView.animateWithDuration(0.7, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.7, options: nil, animations: { [unowned self] () -> Void in
self.cardHeader.alpha = 0
self.cardView.layer.cornerRadius = 0
self.cardView.layoutIfNeeded()
}) { [unowned self] (finished) -> …Run Code Online (Sandbox Code Playgroud) 我的代码很简单;我有一个 UIButton 的出口,button我正在代码中设置它的图像:
let jack = UIImage(named:"jack.png")
self.button.setImage(jack, for:.normal)
Run Code Online (Sandbox Code Playgroud)
问题是这并不像我预期的那样。我希望按钮图像的大小缩小到按钮大小,并且我希望它是模板图像(用继承的色调颜色着色)。相反,我看到的是原始图像,而且它是全尺寸的。这是 iOS 15 中的变化吗?
似乎是这样,因为如果我将项目的部署目标设置为 iOS 14 并在 iOS 14 模拟器上运行它,我确实会得到我期望的行为。
我有一个UIButton在我的应用程序中,而不是文本我给它一个图像,因为它服务器作为缩略图按钮,将在点击时呈现完整的图像.
我看到这个答案似乎是可接受的实现,但是图像小于按钮本身,图像视图实际上并不像contentMode那样填充按钮,或者像图像视图一样,如下所示:

如您所见,使用图像视图可以正确扩展,但使用按钮则不会.(按钮背景设置为蓝色,以便您可以看到它未显示的位置.)
我正在做的代码很简单:
myButton.setImage(UIImage(named: "pokemon"), forState: .Normal)
myButton.imageView!.contentMode = .ScaleAspectFill
Run Code Online (Sandbox Code Playgroud)
如何更改它以使其正确填充?
示例项目: http ://cl.ly/0z2T1A1Z3v3m
ios ×4
uibutton ×3
cocoa-touch ×1
ios15 ×1
ios8 ×1
objective-c ×1
swift ×1
uiimageview ×1
uiview ×1
xcode ×1