7

我需要一些看起来像的东西UIAlertView(相同的背景透明而不是全屏),阻止其他 UI 部分并具有一些自定义内容。此自定义内容是:两个带有标签的复选框和底部的两个按钮 YES/NO。

子类化或自定义UIAlertView看起来没有用(请参阅此答案)而且很危险(代码可能会被 Apple 拒绝)。我正在考虑创建自己的自定义UIView(可能使用UIViewController),但我不知道如何使它看起来和感觉像 UIAlertView。我的意思是我想让它改变它的外观取决于 iOS 版本(iOS7)。

更新:我可以放弃 os 版本依赖,拥有它会很好,但这是附加功能。
主要问题是:有没有一种好方法可以让这种视图看起来和感觉像 UIAlertView 而无需大量工作?直接定制UIAlertView看起来既复杂又危险。

4

4 回答 4

20

我创建了自己的自定义视图,看起来像 iOS UIAlertView 7。使用该技术,您可以为 iOS 6 和 iOS 7 创建自定义警报。为此,我在我的 UIViewController 的 xib 文件中创建了一个 UIView:

在此处输入图像描述

我为此视图添加了一些@property:

// Custom iOS 7 Alert View
@property (nonatomic, weak) IBOutlet UIView *supportViewPopup; // My UIView
@property (nonatomic, weak) IBOutlet UIView *supportViewPopupBackground; // The grey view
@property (nonatomic, weak) IBOutlet UIView *supportViewPopupAction; // The white view with outlets
// Property for customize the UI of this alert (you can add other labels, buttons, tableview, etc.
@property (nonatomic, weak) IBOutlet UIButton *buttonOK;
@property (nonatomic, weak) IBOutlet UIButton *buttonCancel;
@property (nonatomic, weak) IBOutlet UILabel *labelDescription;

在我的 viewDidLoad 上:

- (void)viewDidLoad
{
    [super viewDidLoad];

       // Support View
    self.supportViewPopupAction.layer.cornerRadius = 5.0f;
    self.supportViewPopupAction.layer.masksToBounds = YES;

    // Add Support View
    [self.view addSubview:self.supportViewPopup];

    // Center Support view
    self.supportViewPopup.center = self.view.center;

    // Alpha
    self.supportViewPopup.alpha = 0.0f;
    self.supportViewPopupBackground.alpha = 0.0f;
    self.supportViewPopupAction.alpha = 0.0f;
}

显示 Popup 的操作:

- (IBAction)displayPopup
{
    // Support View
    self.supportViewPopup.alpha = 1.0f;
    self.supportViewPopupBackground.alpha = 0.5f;

    // Animation
    [UIView animateWithDuration:0.5f
                     animations:^{
                         self.supportViewPopupAction.alpha = 1.0f;
                     }];
}

关闭 Popup 的操作:

- (IBAction)dismissModal
{
    // Animation
    [UIView animateWithDuration:0.5f
                     animations:^{
                         self.supportViewPopup.alpha = 0.0f;
                         self.supportViewPopupBackground.alpha = 0.0f;
                         self.supportViewPopupAction.alpha = 0.0f;
                     }];
}

因此,您可以supportViewPopupAction使用按钮、表格视图、标签、集合视图等配置您想要的样式...

我花时间写了这个警报视图的例子。我希望这能帮到您 !

于 2013-10-01T14:47:26.710 回答
1

自定义视图可以传递给 PXAlertView:https ://github.com/alexanderjarvis/PXAlertView

于 2014-08-26T14:12:25.800 回答
0

Some components as UIButtons and UITextFields are going to look different depending on the version, so that's going to be fine, the problem I see is going to be in the view that contains then.

My suggestion is to detect the version where the app is running and then draw the alert view based on that, or just create a custom design that will fit both.

于 2013-10-01T14:36:07.943 回答
0

根据 iOS 版本创建视图!

NSString *version = [[UIDevice currentDevice] systemVersion];
        int major = [version intValue];
        if (major < 7)
            //alert for the iOS 6
    else
    //alert for the iOS 7
于 2013-10-01T14:38:55.177 回答