如何更改目标c中的字体样式和颜色或操作表?

时间:2016-07-12 04:29:21

标签: ios objective-c uiactionsheet uialertcontroller

我有一个行动表' selectedActionSheet'。我使用alertcontroller

创建了actionsheet
UIAlertController *alertController = [selectedActionSheet valueForKey:@"_alertController"];    

现在,我按如下方式更改色调颜色:

if ([alertController isKindOfClass:[UIAlertController class]])
{
   alertController.view.tintColor = [UIColor colorWithRed:0.0/255.0 green:99.0/255.0 blue:65.0/255.0 alpha:1.0];
}    

这会改变色调的颜色,但是当我点击操作表时,我再次看到默认的蓝色。有人可以帮我解决这个问题吗?我是Objective c的新手。

3 个答案:

答案 0 :(得分:1)

Just set the alertController tintColor after the presentViewController i.e.

[self presentViewController:alertController animated:YES completion:nil];

alertController.view.tintColor = [UIColor colorWithRed:0.0/255.0 green:99.0/255.0 blue:65.0/255.0 alpha:1.0];

this will change default as well as highlighted color. Hope this will solved your problem.

If you simply want to present actionSheet, then please try below code-

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Hello Dear" message:@"I'm an actionSheet!" preferredStyle:UIAlertControllerStyleActionSheet];


UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK"
                                                 style:UIAlertActionStyleDefault
                                               handler:^(UIAlertAction *action){
                                                   //add code to make something happen once tapped
                                               }];

[alertController addAction: ok];

UIAlertAction *Cancel = [UIAlertAction actionWithTitle:@"Cancel"
                                             style:UIAlertActionStyleDestructive
                                           handler:^(UIAlertAction *action){
                                               //add code to make something happen once tapped
                                           }];

[alertController addAction: Cancel];

[self presentViewController:alertController animated:YES completion:nil];

alertController.view.tintColor = [UIColor colorWithRed:0.0/255.0 green:99.0/255.0 blue:65.0/255.0 alpha:1.0];

actionSheet will look like this-

enter image description here

答案 1 :(得分:0)

试试此代码

UIActionSheet实际上在iOS 8中使用UIAlertController,并且它有一个私有属性_alertController

SEL selector = NSSelectorFromString(@"_alertController");
if ([actionSheet respondsToSelector:selector])
{
    UIAlertController *alertController = [actionSheet valueForKey:@"_alertController"];
    if ([alertController isKindOfClass:[UIAlertController class]])
    {
        alertController.view.tintColor = [UIColor blueColor];
    }
}

更改所有按钮颜色

[[UIView appearanceWhenContainedIn:[UIAlertController class], nil] setTintColor:[UIColor redColor]];

由于

答案 2 :(得分:0)

对于要设置为警报视图控制器的字体,下面是代码...

UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Your Alert Title" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
NSMutableAttributedString *hogan = [[NSMutableAttributedString alloc] initWithString:@"Your alert description..."];
[hogan addAttribute:NSFontAttributeName
              value:[UIFont systemFontOfSize:50.0]
              range:NSMakeRange(24, 11)];
[alertVC setValue:hogan forKey:@"attributedTitle"];

对于UIAlertController色调颜色......

alertVC.view.tintColor = [UIColor colorWithRed:1.0/255.0 green:99.0/255.0 blue:265.0/255.0 alpha:1.0];