Ios 如何调整UIButton';你的尺寸是多少?
如何调整UIButton的图像大小?我将图像设置为:Ios 如何调整UIButton';你的尺寸是多少?,ios,objective-c,xcode,uibutton,Ios,Objective C,Xcode,Uibutton,如何调整UIButton的图像大小?我将图像设置为: [myLikesButton setImage:[UIImage imageNamed:@"icon-heart.png"] forState:UIControlStateNormal]; 但是,这会将图像填充到完整按钮,如何使图像变小?如果我正确理解您的意图,您需要使用按钮图像边缘插入。比如: myLikesButton.imageEdgeInsets = UIEdgeInsets(top: 30, left: 30, bottom: 3
[myLikesButton setImage:[UIImage imageNamed:@"icon-heart.png"] forState:UIControlStateNormal];
但是,这会将图像填充到完整按钮,如何使图像变小?如果我正确理解您的意图,您需要使用按钮图像边缘插入。比如:
myLikesButton.imageEdgeInsets = UIEdgeInsets(top: 30, left: 30, bottom: 30, right: 30)
我认为,您的图像大小也和按钮大小相同,然后您将图像放在按钮的背景中,如下所示:
[myLikesButton setBackgroundImage:[UIImage imageNamed:@"icon-heart.png"] forState:UIControlStateNormal];
您可能有相同大小的图像和按钮。我希望您理解我的观点。您可以使用
imageEdgeInsets
属性
按钮图像周围矩形的插入页边距或起始页边距
[self.btn setImageEdgeInsets:UIEdgeInsetsMake(6, 6, 6, 6)];
正值将收缩或插入移动的边。负值会扩大或超过这一优势。蒂姆的回答是正确的,但我想补充另一个建议,因为在我的例子中,有一个更简单的解决方案 我想设置
UIButton
图像插入,因为我没有意识到我可以在按钮的UIImageView
上设置内容模式,这样就不需要同时使用UIEdgeInSet和硬编码值。只需访问按钮上的基础imageview并设置内容模式:
myButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
看
Swift 3
myButton.imageView?.contentMode = .scaleAspectFit
yourButtonName.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10)
以下是Swift版本:
myButton.imageEdgeInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
在的帮助下,我能够使用Swift在UIButton
上创建一个扩展,该扩展允许您使用带有额外frame
参数的.setImage()
函数指定图像帧
extension UIButton{
func setImage(image: UIImage?, inFrame frame: CGRect?, forState state: UIControlState){
self.setImage(image, forState: state)
if let frame = frame{
self.imageEdgeInsets = UIEdgeInsets(
top: frame.minY - self.frame.minY,
left: frame.minX - self.frame.minX,
bottom: self.frame.maxY - frame.maxY,
right: self.frame.maxX - frame.maxX
)
}
}
}
使用此选项,如果要将ui按钮的帧设置为CGRectMake(0,0,64,64)
,并将其图像设置为myImage
,帧设置为CGRectMake(8,8,48,48)
,则可以使用
let button: UIButton = UIButton(frame: CGRectMake(0, 0, 64, 64))
button.setImage(
myImage,
inFrame: CGRectMake(8, 8, 48, 48),
forState: UIControlState.Normal
)
您也可以像这样从inteface builder执行此操作
我认为这很有帮助。如果你的图像太大(而且你不能/不想只是把图像缩小),那么将前两个答案结合起来会很好
addButton.imageView?.contentMode = .scaleAspectFit
addButton.imageEdgeInsets = UIEdgeInsetsMake(15.0, 15.0, 15.0, 5.0)
除非图像插入正确,否则图像将在不更改contentMode
Swift 3:
button.setImage(UIImage(named: "checkmark_white"), for: .normal)
button.contentVerticalAlignment = .fill
button.contentHorizontalAlignment = .fill
button.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10)
为Swift 3更新
myButton.imageView?.contentMode = .scaleAspectFit
yourButtonName.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10)
斯威夫特3
我将我的按钮宽度和高度设置为40,我的边沿填充宽度为15。我建议为按钮添加背景色,以查看实际的填充
myButton.backgroundColor = UIColor.gray // sample color to check padding
myButton.imageView?.contentMode = .scaleAspectFit
myButton.imageEdgeInsets = UIEdgeInsetsMake(15, 15, 15, 15)
Swift 4
您需要按照这个特定的顺序使用这两行代码。您只需更改边插入的顶部和底部值
addButton.imageView?.contentMode = .scaleAspectFit
addButton.imageEdgeInsets = UIEdgeInsetsMake(10.0, 0.0, 10.0, 0.0)
一种方法是在如下代码中调整UIImage的大小。注意:此代码仅按高度缩放,但您也可以轻松地将此功能调整为按宽度缩放
let targetHeight = CGFloat(28)
let newImage = resizeImage(image: UIImage(named: "Image.png")!, targetHeight: targetHeight)
button.setImage(newImage, for: .normal)
fileprivate func resizeImage(image: UIImage, targetHeight: CGFloat) -> UIImage {
// Get current image size
let size = image.size
// Compute scaled, new size
let heightRatio = targetHeight / size.height
let newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)
let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)
// Create new image
UIGraphicsBeginImageContextWithOptions(newSize, false, 0)
image.draw(in: rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
// Return new image
return newImage!
}
使用更改图标大小时
UIEdgeInsetsMake(上、左、下、右)
,记住按钮尺寸和UIEdgeInsetsMake处理负值的能力,就像它们是正值一样
示例:两个按钮,高度为100,纵横比为1:1
left.imageEdgeInsets = UIEdgeInsetsMake(40, 0, 40, 0)
right.imageEdgeInsets = UIEdgeInsetsMake(40, 0, 40, 0)
示例1和3相同,因为ABS(100-(40+40))=ABS(100-(60+60))这里是缩放UIButton图像视图的另一种解决方案
button.imageView?.layer.transform = CATransform3DMakeScale(0.8, 0.8, 0.8)
针对Swift>5更新
设置大小:
button.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
设置边距:
button.imageEdgeInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
您是否尝试过使图像变小(LOL)?@CodaFi是正确的,您通常应该提供适当大小的资产,因为它减少了CPU的工作量,使用更少的内存,并且很可能会看起来更好,因为您不会潜在地引入任何缩放伪影…您还可以将按钮设置为图像大小(反之亦然)。为什么要有一个大按钮…里面有一个小图像。只需添加您想要的图像,这就是自定义按钮的用途,或者您可以解释您想要实现的逻辑,让其他人有一个清晰的想法。您只需设置边缘插入,但在许多/所有情况下,这显然取决于按钮的大小。(例如,您希望填充为按钮宽度的四分之一,这样,图像位于按钮大小的二分之一区域中,而不管它有多大,是否正在设置动画等)。显然,您只需在LayoutSubView中执行此操作。令人惊讶的是,没有一个答案提到这一点!你应该if let
,而不是检查frame
是否为nil
,然后。如何对backgroundImage执行相同操作?@Andrea.Ferrando按钮。backgroundImageView.contentMode
不起作用。请节省一些时间,并确保设置的是图像而不是背景图像@SeongLee是对的,你不能用contentMode或EdgeInserts处理背景图像。这对我来说很好。我在Inkscape中创建了一个PDF图标,contentVerticalAlignment/contentHorizontalAlignment
选项使我能够以我希望的方式缩放它。感谢您发布此内容。设置contentHorizontalAlignment
和contentVerticalAlignment
是否有技巧此解决方案在使用PDF资源作为图标时效果非常好。非常感谢。contentVerticalAlignment和contentHorizontalAlignment对我来说确实有用,因为我的UIImageView在UIButton中太小了……因此,对于系统图标/SF符号来说,它们也可以很好地工作——谢谢!从override func layoutSubviews()
调用时,这些操作导致EXC\u BAD\u ACCESS
错误。您保存了我的一天。我只是想在一个按钮内缩放我的图像。没有插图什么都没有,谢谢!手动设置图像插入在调整我的按钮/文本大小时导致问题。此解决方案动态运行效果最佳。这是最佳答案。