Подтвердить что ты не робот

Цвет кнопки UIActionSheet

Как изменить цвет цвета кнопки UIActionSheet?

4b9b3361

Ответ 1

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], все кнопки, кроме кнопки отмены, будут окрашены. Надеюсь, это поможет кому-то!

Ответ 2

Я создал дочерний класс UICustomActionSheet, который позволяет настраивать шрифты, цвета и изображения кнопок внутри UIActionSheet. Это абсолютно безопасно для appstore, вы можете найти код этого класса по следующей ссылке:

https://github.com/gloomcore/UICustomActionSheet

Наслаждайтесь!

Ответ 3

К сожалению, без использования недокументированных API нет официального способа изменить цвет кнопки на странице UIActionSheet. Вы можете настроить его, если вы подклассифицируете элемент управления UIActionSheet.

Смотрите этот пример: http://blog.corywiles.com/customizing-uiactionsheet-buttons

Ответ 4

Мы можем использовать фоновые изображения для этого. Я думаю, что это самый простой способ.

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

Ответ 5

Вы можете легко достичь этого, используя следующий код

Документация по протоколу Apple 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];
        }
    }
}