How do I code a green button in UIActionSheet?

孤街浪徒 提交于 2019-12-10 14:22:43

问题


I am using the code:

  {
        randomstatus=0;
        msg=[[NSString alloc]initWithFormat:@"Good job, do you want to continue?"];
        UIActionSheet *actionSheet=[[UIActionSheet alloc]initWithTitle:msg delegate:self cancelButtonTitle:@"No" destructiveButtonTitle:@"Yes" otherButtonTitles:nil];
        [actionSheet showInView:self.view];
        [actionSheet release];
        [msg release];
    }   

I don't want to change the code, but I need the "destructiveButton" to be green instead of red. Is this possible, or do i need to use a different button?


回答1:


Unfortunately there aren't any officially provided methods for customizing UIActionSheet buttons.

However you can access the subviews of the UIActionSheet (which could break in a future iPhone OS update), or add a new view with a button that covers the original destructive action button (once again, this may break).

While not directly related to changing the color of a button on a UIActionSheet, this previous question: iPhone Disabling UIActionSheet buttons demonstrates a few ways you could add custom views to a UIActionSheet.




回答2:


There is no "standard" way of changing the appearince of the buttons. Any ways you use will essentially be hacks and may break in the future if Apple change the UIActionSheet component. They may also get your app rejected if they upset the App-Store gods.

I think the most future-proof way of acheiving this is to create your own action sheet class from scratch, ie not subclassing UIActionSheet (as this may break in the future). Although this may be a bit more work up-front than some hack, the extra flexibility you'll gain will come in useful in the future.

This shouldn't be too difficult. You'll need a view which is the background for the action sheet, which you can get by taking a screenshot of a standard UIActionSheet and some photoshopping. Then add your custom buttons as sub-views. A bit of animation for bringing up the view and you're done.

I would aim to have your class implement all the methods the UIActionSheet does, as well as firing off the methods UIActionSHeetDelegate expects. This way you'll be able to substitue it in anywhere you'd otherwise use a native UIActionSheet



来源:https://stackoverflow.com/questions/2488878/how-do-i-code-a-green-button-in-uiactionsheet

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