How to programmatically set my UIButton image to 'redraw'?

。_饼干妹妹 提交于 2020-01-05 03:33:15

问题


I am using some vector graphics (.png) for my app. In photoshop, I can zoom in and always see a perfectly crisp boundary for simple geometric shapes, like circles. However, on my iPhone, my same graphics look jaggedy and rough. 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];

回答1:


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!



来源:https://stackoverflow.com/questions/16909753/how-to-programmatically-set-my-uibutton-image-to-redraw

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!