如何在iOS中使用边缘插图? [英] How to use edge insets in iOS?
问题描述
如何使用边缘插图使图像适合按钮内?我不确定UIEdgeInsetsMake()中的参数要使用什么值。我能以某种方式使用按钮的宽度和高度来设置它们吗?
How would I use edge insets to make an image fit inside a button? I'm not sure what values to use for the arguments in UIEdgeInsetsMake(). Could I somehow use the button's width and height to set them?
我希望按钮在未被选择时只是图像。我了解如何执行未选中时部分,但是我不知道如何使用边缘插图,也不知道该术语的确切含义。
I want the button to just be an image when not selected. I understand how to do the "when not selected" part, but I have no idea how to use edge insets nor what the term exactly means.
我在时刻:
image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)];
[button setImage:image forState:!UIControlStateSelected];
此外,一旦按钮被选中,我该如何恢复为不使用图像?
Also, how would I revert to using no image once the button becomes selected?
推荐答案
我想我明白你的意思。如果我误解了您的问题,请告诉我。
I think I am getting what you are saying. Please let me know if I am misunderstanding your question.
说您有 myImage.png
。初始化如下:
[button setImage:[UIImage imageNamed:@"myImage.png"] forState:UIControlStateNormal];
然后,假设您已经为Interface Builder中的按钮连接了IBAction:
Then, assuming you have connected an IBAction for the button in Interface Builder:
- (IBAction)myButtonWasPressed:(UIButton *)sender
{
[sender setImage:nil];
}
这篇关于如何在iOS中使用边缘插图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!