iPad上的UIActionSheet背景颜色

4

我已经有一段时间在处理一个问题。由于我没有真正找到解决方案,因此我希望这里的某个人能够帮助我。

我有一个UIActionSheet,我想让它具有不同的背景颜色。 使用

[[myAlert layer] setBackgroundColor:[UIColor redColor].CGColor];

我能够改变大部分警报的颜色。 这就是它的外观:

This is how it looks

这是我初始化UIActionSheet的方式:

    UIActionSheet *styleAlert = [[UIActionSheet alloc] initWithTitle:AMLocalizedString(@"SELECT_PICTURE_TITLE", @"Überschrift des Actionsheets")
                                                            delegate:self
                                                   cancelButtonTitle:AMLocalizedString(@"CANCEL_BUTTON_TITLE", @"Abbrechen butten")
                                              destructiveButtonTitle:nil
                                                   otherButtonTitles:AMLocalizedString(@"TAKE_PICTURE_BUTTON_TITLE", @"Bild aufnehmen button"),
                                 AMLocalizedString(@"CAMERA_ROLL_BUTTON_TITLE", @"Aus Bibliothek auswählen"), nil];

    // use the same style as the nav bar
    [styleAlert showInView:self.parentViewController.tabBarController.view];
    //[styleAlert showInView:[self.navigationController view] ];
    [styleAlert release];

并且

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
    actionSheet.layer.backgroundColor = GLOBAL_TINT_COLOR.CGColor;
}

我不知道如何设置边框颜色相同。有什么想法吗?
提前感谢!

UIAlertView的背景似乎是一张图片。 https://dev59.com/gHNA5IYBdhLWcg3wpfiu - Larme
请参考此帖子:https://dev59.com/vnM_5IYBdhLWcg3w4njb这对你会很有用。 - Saurabh Shukla
感谢您的回复!不幸的是,无论我使用图像还是设置层的背景颜色,都无法删除边框。 - pmk
你尝试过这样的方法吗?https://dev59.com/v0jSa4cB1Zd3GeqPJ_eX - ryanwils
另外,我之前在iPad上使用UIActionSheet时遇到了问题,我认为最好的方法是使用UIPopoverController。 - ryanwils
2个回答

1

您无法以不同的颜色重绘边框,因此只需将边框移除并添加自己的边框:

- (void)didPresentActionSheet:(UIActionSheet *)actionSheet {
    UIView *contentView = actionSheet.superview;
    UIView *popoverView = contentView.superview;

    UIView *chromeView;
    for (UIView *v in [popoverView subviews]) {
        if (v.subviews.count == 3) {
            chromeView = v;
            break;
        }
    }

    for (UIView *backgroundComponentView in [chromeView subviews]) {
        backgroundComponentView.hidden = YES;

        CGRect componentFrame = backgroundComponentView.frame;  // add your view with this frame
    }
}

请注意,这在*will*PresentActionSheet中不起作用,因为此时actionSheet没有设置superview

这正是我所需要的。虽然它看起来还不够好,但是backgroundView已经被移除了,这就是我的问题所在。我只需要添加自己的背景和颜色等内容。完成后我会发布一些代码。非常感谢,这真的很有帮助! - pmk

0

这个可以用于iPhone主题,很可能也适用于iPad。

    - (void)willPresentActionSheet:(UIActionSheet *)actionSheet {

        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed@""]];
        imageView.frame = CGRectMake(0, 0, actionSheet.frame.size.width, actionSheet.frame.size.height);
        [actionSheet insertSubview:imageView atIndex:0];
        NSArray *subviews = [actionSheet subviews];
        for (UIView *v in subviews) {
            if ([v isKindOfClass:[UIButton class]]) {
//Theme the buttons on the action sheet
                UIButton *b = (UIButton *)v;
            }
            else if ([v isKindOfClass:[UILabel class]]) {
                UILabel *l = (UILabel *)v;
//Theme the label at the top of the action sheet
            }
        }
    }

谢谢你的回答。不幸的是,你的代码和我的完全一样,在iPhone上可以工作,但在iPad上却不行。未着色的边框仍然存在 :( - pmk
在这段代码中,使用NSLog记录子视图,然后尝试查找图像。我猜应该有两个,在顶部我主题化了第一个,但在iPad上必须有两个,所以你需要隐藏它或为其准备一张图片。 - Maximilian Litteral

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接