简体   繁体   English

如何以编程方式将我的UIButton图像设置为“重绘”?

[英]How to programmatically set my UIButton image to 'redraw'?

I am using some vector graphics (.png) for my app. 我正在为我的应用程序使用一些矢量图形(.png)。 In photoshop, I can zoom in and always see a perfectly crisp boundary for simple geometric shapes, like circles. 在photoshop中,我可以放大并始终看到简单的几何形状(如圆形)的清晰边界。 However, on my iPhone, my same graphics look jaggedy and rough. 但是,在我的iPhone上,相同的图形看起来参差不齐且粗糙。 I found that if I change the image fill type to redraw', this helps the problem. 我发现如果将图像填充类型更改为“重绘”,则可以解决此问题。

So, can someone please confirm if using redraw is the correct solution in this situation? 因此,有人可以确认在这种情况下使用重绘是否是正确的解决方案吗? Also, how can I make the image for a programmatically defined button use 'redraw' instead of the default (which I think is 'fill')? 另外,如何使通过程序定义的按钮的图像使用“重新绘制”而不是默认值(我认为是“填充”)?

Here is my current button's code: 这是我当前按钮的代码:

UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn1 setTitle:@"Cool title" forState:UIControlStateNormal];
[btn1 setFrame:CGRectMake(7, 7, 150, 160)];
[btn1 setImage:[UIImage imageNamed:@"myGraphic.png"] forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(selectFav) forControlEvents:UIControlEventTouchUpInside];
[_scroller addSubview:btn1];

remove this line: 删除此行:

 UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

add this line: 添加此行:

 UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];

set content mode: 设置内容模式:

 [btn1 setContentMode:UIViewContentModeRedraw];

Hope this answers all your concerns! 希望这能回答您的所有问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM