GithubHelp home page GithubHelp logo

isabella232 / sdcalertview Goto Github PK

View Code? Open in Web Editor NEW

This project forked from badoo/sdcalertview

0.0 0.0 0.0 779 KB

A UIAlertView clone with added functionality, including a contentView property

License: MIT License

Ruby 0.86% Objective-C 99.14%

sdcalertview's Introduction

#SDCAlertView

SDCAlertView doesn't just look like a system alert in terms of user interface elements, it is the result of completely reverse-engineering UIAlertView. View hierarchy, labels, buttons, animations, user interaction; everything has been looked at and incorporated as much as possible.

You can think of SDCAlertView as UIAlertView on steroids. It has added functionality such as a contentView property and block syntax, while still keeping the UIAlertView look.

Animated GIF showing alert

Installation

The easiest way to install is, of course, by using CocoaPods. The name of the pod is SDCAlertView.

If you're not using CocoaPods, you need:

  • SDCAlertView.{h,m}
  • SDCAlertViewTransitioning.h
  • SDCAlertViewCoordinator.{h,m}
  • SDCAlertViewController.{h,m}
  • SDCAlertViewContentView.{h,m}
  • SDCAlertViewBackgroundView.{h,m}
  • SDCIntrinsicallySizedView.{h,m}

The project also depends on RBBAnimation and SDCAutoLayout. These dependencies are automatically handled for you if you use CocoaPods.

Usage

SDCAlertView is for use in iOS 7 only. It will not work properly on iOS 6.1 or below. Using SDCAlertView is very simple: just import SDCAlertView.h and use it as you would UIAlertView.

Basic

Showing a basic SDCAlertView alert looks just like showing a basic UIAlertView alert:

SDCAlertView *alert = [[SDCAlertView alloc] initWithTitle:@"Title"
												  message:@"This is a message"
												 delegate:self
										cancelButtonTitle:@"Cancel"
										otherButtonTitles:@"OK", nil];
[alert show];

Or you can use one of the convenience methods:

[SDCAlertView alertWithTitle:@"Title" message:@"This is a message" buttons:@[@"OK"]];

Block syntax

Block syntax saves you from having to use a delegate:

SDCAlertView *alert = [[SDCAlertView alloc] initWithTitle:@"Title"
												  message:@"This is a message"
												 delegate:nil
										cancelButtonTitle:@"Cancel"
										otherButtonTitles:@"OK", nil];
[alert showWithDismissHandler:^(NSInteger buttonIndex) {
	NSLog(@"Tapped button: %@", @(buttonIndex));
}];

contentView

Of course, you're not using SDCAlertView's full potential unless you are using the contentView:

SDCAlertView *alert = [[SDCAlertView alloc] initWithTitle:@"Title"
												  message:@"This is a message"
												 delegate:self
										cancelButtonTitle:@"Cancel"
										otherButtonTitles:@"OK", nil];
		
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] init];
spinner.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
[spinner setTranslatesAutoresizingMaskIntoConstraints:NO];
[spinner startAnimating];

[alert.contentView addSubview:spinner];
[spinner sdc_horizontallyCenterInSuperview];
[spinner sdc_verticallyCenterInSuperviewWithOffset:SDCAutoLayoutStandardSiblingDistance];
[alert show];

Additional delegate methods

You can also use the alertView:shouldDismissWithButtonIndex: and alertView:shouldDeselectButtonAtIndex: to prevent an alert from dismissing:

- (void)showAlert {
	SDCAlertView *alert = [[SDCAlertView alloc] initWithTitle:title
													  message:message
													 delegate:self
											cancelButtonTitle:@"Cancel"
											otherButtonTitles:nil];
	[alert show];
}

- (BOOL)alertView:(SDCAlertView *)alertView shouldDismissWithButtonIndex:(NSInteger)buttonIndex {
	return NO;
}

- (BOOL)alertView:(SDCAlertView *)alertView shouldDeselectButtonAtIndex:(NSInteger)buttonIndex {
	return YES;
}

This will deselect the cancel button when it's tapped, but it won't actually dismiss the alert. Useful for password-like alerts that you don't want dismissed until the right password is entered.

Styling and appearance

SDCAlertView uses the tintColor for buttons and any subviews you add to the contentView. If you are looking for more customizations, you can use UIAppearance to style alerts (per instance or all at once):

[[SDCAlertView appearance] setTitleLabelFont:[UIFont boldSystemFontOfSize:22]];
[[SDCAlertView appearance] setMessageLabelFont:[UIFont italicSystemFontOfSize:14]];
[[SDCAlertView appearance] setNormalButtonFont:[UIFont boldSystemFontOfSize:12]];
[[SDCAlertView appearance] setSuggestedButtonFont:[UIFont italicSystemFontOfSize:12]];
[[SDCAlertView appearance] setTextFieldFont:[UIFont italicSystemFontOfSize:12]];
[[SDCAlertView appearance] setButtonTextColor:[UIColor grayColor]]; // will always override the tintColor
[[SDCAlertView appearance] setTextFieldTextColor:[UIColor purpleColor]];
[[SDCAlertView appearance] setTitleLabelTextColor:[UIColor greenColor]];
[[SDCAlertView appearance] setMessageLabelTextColor:[UIColor yellowColor]];

If you're feeling particularly adventurous, SDCAlertView makes it very easy to customize the way alerts are presented and dismissed. Set the transitionCoordinator property to a custom class that conforms to SDCAlertViewTransitioning, and implement the three protocol methods. For more detailed instructions, see SDCAlertViewTransitioning.h.

Behavior different from UIAlertView

Unfortunately, there are a few things that can't or won't be duplicated:

  • Special interaction with the system. The system does not consider instances of SDCAlertView actual alerts, which means that, for example, the alertViewCancel: method from SDCAlertViewDelegate will never be called.
  • UITextField placeholders in different languages. "Login" and "Password" are entered as localized strings, but they aren't actually translated.
  • Some behavior is purposely not ported from UIAlertView. These cases are discussed in SDCAlertView.h.

New in 1.4

What's New:

  • Added the ability to position a two-button alert vertically as opposed to horizontally
  • Added attributedTitle and attributedMessage properties

Bug Fixes:

  • Auto-layout doesn't complain anymore when using [[SDCAlertView alloc] init]
  • Fixes a bug that would not show correct button titles in certain alert configurations
  • Instead of clipping button text, it now reduces the size of text on buttons appropriately

Support

I'm pretty active on Stack Overflow, so please use that if you have any questions. You can also use Twitter to contact me directly.

If you are experiencing bugs, feel free to post an issue or submit a pull request. I don't bite, promise!

Credits

Some credits are in order:

  • Robert Böhnke (@robb): RBBAnimation
  • Lee McDermott (@lmcd) for reverse-engineering the showing and dismissing animations.
  • César Castillo (@JagCesar) for the great idea of using a UIToolbar for easy live blurring (used in earlier versions of SDCAlertView).

And everyone else who contributed by reporting issues, creating pull requests, or in some other way!

sdcalertview's People

Contributors

sberrevoets avatar damirdavletov avatar scott90 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.