2010-11-22 102 views

回答

34

的iOS 8(UIAlertController)

這是超級簡單的做,如果您使用的是UIAlertController。只需更改UIAlertController視圖上的色調顏色即可。

[alertController.view setTintColor:[UIColor red]; 

的iOS 7(UIActionSheet)

我通過使用這種簡單的方法成功改變文本的顏色。

- (void) changeTextColorForUIActionSheet:(UIActionSheet*)actionSheet { 
    UIColor *tintColor = [UIColor redColor]; 

    NSArray *actionSheetButtons = actionSheet.subviews; 
    for (int i = 0; [actionSheetButtons count] > i; i++) { 
     UIView *view = (UIView*)[actionSheetButtons objectAtIndex:i]; 
     if([view isKindOfClass:[UIButton class]]){ 
      UIButton *btn = (UIButton*)view; 
      [btn setTitleColor:tintColor forState:UIControlStateNormal]; 

     } 
    } 
} 

確保運行此你叫

[actionSheet showInView]; 

如果你之前[showInView]調用它,所有的按鈕,但取消按鈕將被着色。希望這可以幫助別人!

1

不幸的是,沒有使用未記錄的API,沒有官方的方法來改變UIActionSheet上的按鈕顏色。如果您繼承UIActionSheet控件,您可能可以自定義這個。

見這個例子:http://blog.corywiles.com/customizing-uiactionsheet-buttons

+0

你有沒有這樣做實現呢?任何示例代碼都會非常有幫助。 – Abhinav 2010-11-22 17:47:08

+0

查看我上面發佈的鏈接。 – 2010-11-22 17:50:23

+0

非常感謝你! – Abhinav 2010-11-22 18:21:18

1

我們可以使用背景圖像來做到這一點。我認爲這是最簡單的方法。

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Actionsheet" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil]; 
     [actionSheet addButtonWithTitle:@"Button 1"]; //Blue color 
     [actionSheet addButtonWithTitle:@"Button 2"]; 
     [actionSheet addButtonWithTitle:@"Cancel"]; 
     [actionSheet addButtonWithTitle:nil]; 
     [actionSheet setCancelButtonIndex:2]; 
     [actionSheet setDestructiveButtonIndex:1]; 
     [actionSheet showInView:self.view]; 
     UIButton *button = [[actionSheet subviews] objectAtIndex:1]; 
     UIImage *img = [button backgroundImageForState:UIControlStateHighlighted];//[UIImage imageNamed:@"alert_button.png"]; 
     [button setBackgroundImage:img forState:UIControlStateNormal]; 
0

您可以輕鬆地通過使用下面的代碼

蘋果UIActionSheetDelegate協議文檔

https://developer.apple.com/library/ios/documentation/uikit/reference/UIModalViewDelegate_Protocol/UIActionSheetDelegate/UIActionSheetDelegate.html

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet 
{ 
    for (UIView *_currentView in actionSheet.subviews) 
    { 
     if ([_currentView isKindOfClass:[UIButton class]]) 
     {  
      UIButton *button = (UIButton *)_currentView; 
      [button setTitleColor:YOUR_COLOR forState:UIControlStateNormal]; 
     } 
    } 
}