自定义视图,看起来像UIAlertView [英] Custom view which looks like UIAlertView

查看:81
本文介绍了自定义视图,看起来像UIAlertView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need something what looks like UIAlertView (same background transparent and not full screen), blocks other UI parts and has some custom content. This custom content are: two check-boxes with labels and two buttons YES/NO at the bottom.

子分类或自定义UIAlertView似乎没有用(请参阅此答案),这很危险(代码可以被Apple拒绝).我当时正在考虑创建自己的自定义UIView(可能与UIViewController一起使用),但是我不知道如何使其外观和感觉像UIAlertView.我的意思是说我想使它的外观取决于iOS版本(iOS7).

Sub-classing or customizing UIAlertView doesn't looks useful (see this answer) and it is dangerous (code can be rejected by Apple). I was thinking to create own custom UIView (possible with UIViewController), but I have no idea how to make it look and feel like UIAlertView. I mean I'd like to make it that it changes its appearance dependent on iOS version (iOS7).

更新: 我可以放弃os版本的依赖关系,这很高兴,但这是附加功能.
主要问题是:是否有一个很好的方法可以使这种视图看起来和感觉像UIAlertView,而无需进行大量工作?直接自定义UIAlertView看起来很复杂且危险.

update: I can abandon os version dependency, it would be nice to have, but this is additional feature.
The main question is: is there a good way to make such view which will look and feel like UIAlertView without large amount of work? Customizing UIAlertView directly looks complicated and dangerous.

推荐答案

我创建了自己的自定义视图,外观类似于iOS UIAlertView7.使用该技术,您可以为iOS 6和iOS 7创建自定义警报. 为此,我在UIViewController的xib文件中创建了一个UIView:

I created my own custom view to look like iOS UIAlertView 7. With that technique you can create a custom alert for both iOS 6 and iOS 7. For that, I created a UIView in my xib file of my UIViewController :

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

I added some @property for this view :

// 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上:

On my 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;
}

显示弹出窗口的操作:

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

解散弹出窗口的动作:

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

因此,您可以通过按钮,表格视图,标签,集合视图等来随意配置supportViewPopupAction.

So, with that you can configure your supportViewPopupAction like you want with buttons, table view, labels, collection view, etc...

我花了一些时间编写此警报视图示例.希望对您有帮助!

I spent time to write this example of alert view. I hope this will help you !

这篇关于自定义视图,看起来像UIAlertView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆