简体   繁体   中英

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

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];

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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