7

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.

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).

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.

Community
  • 1
  • 1
Marek R
  • 32,568
  • 6
  • 55
  • 140
  • While this is perfectly possible, I'd recommend finding a less intrusive and less complicated way to do this. Something like this is a reasonable amount of work. – Linuxios Oct 01 '13 at 14:37

4 Answers4

20

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 :

enter image description here

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;

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

Action to display 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;
                     }];
}

Action to dismiss Popup :

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

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 !

Jordan Montel
  • 8,227
  • 2
  • 35
  • 40
  • But the `UILabel` does not resize itself as it does in `UIAlertView` – Dinesh Jan 21 '14 at 03:25
  • to do that you need to play with auto-layout and auto-sizing :) – Jordan Montel Jan 21 '14 at 08:57
  • How did you make the button lines? – Raphael Ayres Apr 16 '14 at 04:08
  • Nevermind... Got it... Are the the extra views... 1px wide... clever... very clever... – Raphael Ayres Apr 16 '14 at 04:25
  • 1
    I was looking for better solution for a long time, but looks like that multiple xib files for each look and feel is only good solution. Thanks. – Marek R May 06 '14 at 08:38
  • @RaphaelAyres The Lines can be made with a UIView with 1 Pixel height or width, depending on your needs. – Mohamad Kaakati Oct 06 '15 at 16:46
  • Henceforth, you can now use UIAlertController. Nice update by Apple ;) – Jordan Montel Oct 07 '15 at 11:15
  • this is not a UIAlertView like, you need do add the view to the controller, the power of alert view is you don't need to give him the context(view controller in our case) the only thing you need to do in alertview is call "show", so you can call it from anywhere. – Blacky Apr 22 '16 at 15:44
  • Can I have complete source code of this program? because in side menus it showing something like in storyboard, whereas in answer it stated "UIVIew in my xib file", and in our xib file it does not shows view's hierarchy, So I just wants to see the views and subviews hierarchy and their connections. If I've its source code(complete code along with xib or storyboard) it'll be highly appreciated. – Faiz Fareed Dec 19 '18 at 19:35
  • It's from 2013! I do not have this code anymore... sorry – Jordan Montel Dec 20 '18 at 09:18
1

Custom views can be passed to PXAlertView: https://github.com/alexanderjarvis/PXAlertView

Adam W. Dennis
  • 476
  • 4
  • 8
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.

Antonio MG
  • 20,382
  • 3
  • 43
  • 62
0

Creat view depending on the iOS versions!

NSString *version = [[UIDevice currentDevice] systemVersion];
        int major = [version intValue];
        if (major < 7)
            //alert for the iOS 6
    else
    //alert for the iOS 7
Adnan Aftab
  • 14,377
  • 4
  • 45
  • 54
  • this is crappy way of checking iOS version. There is better way: `NSFoundationVersionNumber` see [documentation](https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/miscellaneous/Foundation_Constants/Reference/reference.html#//apple_ref/doc/uid/TP40003793-CH3g-SW144). Anyway this approach requires lots of manual coding. – Marek R May 06 '14 at 08:32