CocoaPods trunk is moving to be read-only. Read more on the blog, there are 19 months to go.

RTAlertView 0.0.7

RTAlertView 0.0.7

TestsTested
LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Dec 2014

Maintained by Roland Tecson.



  • By
  • Roland Tecson

UIAlertView replacement for iOS 7 and iOS 6

Highlights

  • Customizable alert view that looks identical to the iOS 7 system alert view
  • Supports both iOS 6 and iOS 7
  • Same API as UIAlertView
  • Translucent gaussian blur background in iOS 7 (translucent white background in iOS 6)
  • Dismiss alert view when app goes to background (configurable)
  • Dims tintColor of other visible controls when alert view is displayed
  • Parallax effect similar to UIAlertView
  • Support for Swift bridging (See Swift Bridging)

Description

RTAlertView is a customizable alert view that looks identical to the iOS 7 system alert view. It implements the same interface as UIAlertView, but it also allows you to customize the fonts and colours used in the alert view.

RTAlertView

The alert uses auto layout to position itself in the center of the screen, and will automatically adjust its position when the device orientation changes, or when the keyboard is displayed.

The following properties, in addition to those defined for UIAlertView, are defined in RTAlertView:

@property (strong, nonatomic) UIColor *titleColor;
@property (strong, nonatomic) UIFont *titleFont;
@property (strong, nonatomic) UIColor *messageColor;
@property (strong, nonatomic) UIFont *messageFont;
@property (strong, nonatomic) UIColor *cancelButtonColor;
@property (strong, nonatomic) UIFont *cancelButtonFont;
@property (strong, nonatomic) UIColor *otherButtonColor;
@property (strong, nonatomic) UIFont *otherButtonFont;
@property (strong, nonatomic) UIFont *textFieldPlaceholderFont;

If no values are specified for these properties, the default colours and fonts used will be the same as the ones in UIAlertView.

The highlight colour of a button is automatically derived from the button's colour.

RTAlertView one button highlighted

Similarly to UIAlertView, if two buttons are defined, they are laid out side by side.

RTAlertView two buttons

If three or more buttons are defined, they are laid out vertically.

RTAlertView four buttons

Similarly to UIAlertView, if alertViewStyle were set to either UIAlertViewStyleSecureTextInput or UIAlertViewStylePlainTextInput, a single text field will be displayed.

RTAlertView single text field

If alertViewStyle were set to UIAlertViewStyleLoginAndPasswordInput, two text fields will be displayed.

RTAlertView double text fields

By default, RTAlertView will automatically dismiss itself when the app goes to the background. This is as per Apple's recommendation so that the user will not be presented with an alert message that may be out of context when the app is resumed. You may override this behaviour by setting the property dismissesWhenAppGoesToBackground to NO (default value is YES).

RTAlertView dims the tintColor of the all the visible controls in your app (at least those that support it), similar to the iOS 7 UIAlertView. RTAlertView also implements the parallax effect present in the iOS 7 UIAlertView.

Swift Bridging

Swift does not support direct bridging to Objective C variable arguments methods. Unfortunately, the designated initializer for UIAlertView (and hence RTAlertView as it tries to mirror UIAlertView's public API) is a variable arguments method.

    - (id)initWithTitle:(NSString *)title
                message:(NSString *)message
               delegate:(id)delegate
      cancelButtonTitle:(NSString *)cancelButtonTitle
      otherButtonTitles:(NSString *)otherButtonTitles, ...;

To get around this, a secondary initializer is provided by RTAlertView that takes a fixed number of parameters. This initializer only takes one otherButtonTitle (which may be nil if one does not exist). If additional other button titles are required, the method -addButtonTitle: may be used.

    - (id)initWithTitle:(NSString *)title
                message:(NSString *)message
               delegate:(id)delegate
      cancelButtonTitle:(NSString *)cancelButtonTitle
       otherButtonTitle:(NSString *)otherButtonTitle;

Installation & Use

For use in your app, install with Cocoapods

  1. Create or edit your Podfile and add the following line pod 'RTAlertView', '0.0.6'
  2. Perform a pod install
  3. Add the line #import <RTAlertView.h> to your class
  4. Remember to open the <App>.xcworkspace file (not <App>.xcodeproj) in Xcode from here onwards

To run the test application

  1. Clone the git repository, git clone https://github.com/rtecson/RTAlertView.git
  2. cd RTAlertView
  3. Run pod install, pod install (if cocoapods is not installed, see http://http://beta.cocoapods.org)
  4. Run Xcode 5 (or higher) and open RTAlertView.xcworkspace

Usage

    // Alloc and init RTAlertView, button titles may also be set here
    RTAlertView *customAlertView = [[RTAlertView alloc] initWithTitle:@"Test"
                                                              message:@"Message here"
                                                             delegate:nil
                                                    cancelButtonTitle:nil
                                                    otherButtonTitles:nil];

    // Conforms to the RTAlertViewDelegate protocol
    customAlertView.delegate = self;

    // Default is UIAlertViewStyleDefault
    customAlertView.alertViewStyle = UIAlertViewStylePlainTextInput;

    // Add other buttons
    for (int i=0; i<kNumberOfOtherButtons; i++)
    {
        [customAlertView addButtonWithTitle:[NSString stringWithFormat:@"Button %d", i]];
    }

    // Add cancel button
    customAlertView.cancelButtonIndex = [customAlertView addButtonWithTitle:@"Done"];

    // Set button colours
    customAlertView.otherButtonColor = kCustomColor;
    customAlertView.cancelButtonColor = kCustomColor;

    // Display alert view
    [customAlertView show];

Credits

License

RTAlertView is written by Roland Tecson. It is licensed under the MIT License.

If you use RTAlertView in one of your apps, please send me a quick note. I'd love to hear about it.