Customizing the buttons on a UIAlertView

泪湿孤枕 提交于 2020-01-21 10:11:45

问题


This is the current way i'm customizing my buttons:

UIAlertView *av = [[UIAlertView alloc] init];

[av addButtonWithTitle:@""];
UIButton *yesButton = [av.subviews lastObject];

[av show];

[yesButton setImage:[UIImage imageNamed:@"test.png"] forState:UIControlStateNormal];

The problem with this is that the original view is still visible around the image I set for the button. It doesn't completely encapsulate the image. Here's an example of what I have so far:

https://www.dropbox.com/s/htb9pfihwmel5oo/testimage.png

Is there anyway to make it so that the image completely takes up the entire button?


回答1:


If you wan't something that's not as robust as Sly Raskal's answer and just want a quick hack, that works similar to your existing code. You could do something like this..Of course you can change the new buttons frame to match how you want it. Also, you would have to define the UIAlertView as a variable to so you have a reference to it, and dismiss it in your someAction: method.

UIAlertView *av = [[UIAlertView alloc] init];
[av addButtonWithTitle:@""];
UIButton *yesButton = [av.subviews lastObject];
[yesButton setHidden:YES];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[av addSubview:button];
[av show];

[button setImage:[UIImage imageNamed:@"test.png"] forState:UIControlStateNormal];
[button addTarget:av action:@selector(someAction:) forControlEvents:[yesButton allControlEvents]];
[button setFrame:yesButton.frame];



回答2:


If in this case all you are doing is styling the buttons to look different, but in a simple fashion, I would follow this tutorial to accomplish the effect you are looking for:

http://mobile.tutsplus.com/tutorials/iphone/ios-sdk-uialertview-custom-graphics/

If the application you are creating is destined for the App Store, I just want to also mention that whatever changes you implement, be sure it doesn't use any private API's or goes against anything documented in the HIG. Either of these circumstances is surely going to red flag your app for rejection.



来源:https://stackoverflow.com/questions/10828977/customizing-the-buttons-on-a-uialertview

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