LoginSignup
18
18

More than 5 years have passed since last update.

【追記あり】iOS7でも使えてカスタマイズもできるAlertController - MSAlertController

Last updated at Posted at 2014-11-22

はじめに

iOS8からUIAlertViewUIActionSheetがdeprecatedになってしまったので、iOS8でアラートなどを表示する際はUIAlertControllerを使うことになりました。
しかしながらUIAlertControllerはiOS8以降でしか使えないため、ソースコード内にiOS8以上とiOS8未満の分岐を記述することが必要になってきます。

解決方法

MSAlertControllerを使うことでiOS7でもUIAlertControllerと同じような動作を同じようなコードで実現することができます。
alert.png

action_sheet.png

インストール方法

・Cocoapods

Podfileにpod 'MSAlertController'を追記し、pod installを行ってください。

・マニュアル

ここからMSAlertControllerのリポジトリをダウンロードし、MSAlertController/の中身を自身のプロジェクトにコピーしてください。

※どちらのインストール方法でも、QuartzCore.frameworkが必須になります。

使い方

まず#import "MSAlertController.h"をしてください。

Alertを表示
MSAlertController *alertController = [MSAlertController alertControllerWithTitle:@"MSAlertController" message:@"This is MSAlertController." preferredStyle:MSAlertControllerStyleAlert];

MSAlertAction *action = [MSAlertAction actionWithTitle:@"Cancel" style:MSAlertActionStyleCancel handler:^(MSAlertAction *action) {
    NSLog(@"Cancel action tapped %@", action);
}];
[alertController addAction:action];

MSAlertAction *action2 = [MSAlertAction actionWithTitle:@"Destructive" style:MSAlertActionStyleDestructive handler:^(MSAlertAction *action) {
    NSLog(@"Destructive action tapped %@", action);
}];
[alertController addAction:action2];

MSAlertAction *action3 = [MSAlertAction actionWithTitle:@"Default" style:MSAlertActionStyleDefault handler:^(MSAlertAction *action) {
    NSLog(@"Default action tapped %@", action);
}];
[alertController addAction:action3];

[alertController addTextFieldWithConfigurationHandler:nil];

[self presentViewController:alertController animated:YES completion:nil];

UITextFieldを表示させたい場合は

- (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textField))configurationHandler;

を呼び出すことで表示が可能をなります。

Action Sheetを表示
MSAlertController *alertController = [MSAlertController alertControllerWithTitle:@"MSAlertController" message:@"This is MSAlertController." preferredStyle:MSAlertControllerStyleActionSheet];
MSAlertAction *action = [MSAlertAction actionWithTitle:@"Cancel" style:MSAlertActionStyleCancel handler:^(MSAlertAction *action) {
    NSLog(@"Cancel action tapped %@", action);
}];
[alertController addAction:action];

MSAlertAction *action2 = [MSAlertAction actionWithTitle:@"Destructive" style:MSAlertActionStyleDestructive handler:^(MSAlertAction *action) {
    NSLog(@"Destructive action tapped %@", action);
}];
[alertController addAction:action2];

MSAlertAction *action3 = [MSAlertAction actionWithTitle:@"Default" style:MSAlertActionStyleDefault handler:^(MSAlertAction *action) {
    NSLog(@"Default action tapped %@", action);
}];
[alertController addAction:action3];

[self presentViewController:alertController animated:YES completion:nil];

※ Action Sheetの場合はUITextFieldの表示はできません。
MSAlertActionStyleCancelは複数addすることはできません。

カスタマイズ

Alert及びAction Sheetに表示されるタイトル、メッセージ、ボタンのフォント、サイズ、色をカスタマイズできます。

・タイトル、メッセージ

MSAlertControllerには下記のプロパティが用意されています。

@property (strong, nonatomic) UIColor *titleColor;
@property (strong, nonatomic) UIFont *titleFont;
@property (strong, nonatomic) UIColor *messageColor;
@property (strong, nonatomic) UIFont *messageFont;
@property (assign, nonatomic) BOOL enabledBlurEffect;

alert_controller_custom.png

MSAlertController *alertController = [MSAlertController alertControllerWithTitle:@"MSAlertController" message:@"This is MSAlertController." preferredStyle:MSAlertControllerStyleAlert];
alertController.titleColor = [UIColor blueColor];
alertController.titleFont = [UIFont fontWithName:@"Baskerville-BoldItalic" size:20.0f];
alertController.messageColor = [UIColor greenColor];
alertController.messageFont = [UIFont fontWithName:@"Baskerville-BoldItalic" size:18.0f];
ボタン

MSAlertActionには下記のプロパティが用意されています。

@property (strong, nonatomic) UIColor *titleColor;
@property (strong, nonatomic) UIFont *font;

action_custom.png

MSAlertAction *action = [MSAlertAction actionWithTitle:@"Cancel" style:MSAlertActionStyleCancel handler:^(MSAlertAction *action) {
    //Write a code for this action.
}];
action.titleColor = [UIColor redColor];
action.font = [UIFont fontWithName:@"Baskerville-BoldItalic" size:18.0f];
[alertController addAction:action];

今後

現状でObjective-Cにしか対応していませんが、バージョンアップでSwiftにも対応していく予定です。

追記

2014-11-24 19:18

カスタマイズのプロパティを追加しました。

For Alert Controller
@property (strong, nonatomic) UIColor *backgroundColor;
@property (assign, nonatomic) CGFloat alpha;
@property (strong, nonatomic) UIColor *alertBackgroundColor;
@property (strong, nonatomic) UIColor *separatorColor;
For Action
@property (strong, nonatomic) UIColor *normalColor;
@property (strong, nonatomic) UIColor *highlightedColor;

combination.png

サンプルコード
MSAlertController *alertController = [MSAlertController alertControllerWithTitle:@"MSAlertController" message:@"This is MSAlertController." preferredStyle:MSAlertControllerStyleAlert];
alertController.alertBackgroundColor = [UIColor lightGrayColor];
alertController.backgroundColor = [UIColor blueColor];
alertController.alpha = 0.3f;
alertController.separatorColor = [UIColor redColor];

MSAlertAction *action = [MSAlertAction actionWithTitle:@"Cancel" style:MSAlertActionStyleCancel handler:^(MSAlertAction *action) {
    NSLog(@"Cancel action tapped %@", action);
}];
action.normalColor = [UIColor blackColor];
action.highlightedColor = [UIColor yellowColor];
[alertController addAction:action];

MSAlertAction *action2 = [MSAlertAction actionWithTitle:@"Destructive" style:MSAlertActionStyleDestructive handler:^(MSAlertAction *action) {
    NSLog(@"Destructive action tapped %@", action);
}];
[alertController addAction:action2];

MSAlertAction *action3 = [MSAlertAction actionWithTitle:@"Default" style:MSAlertActionStyleDefault handler:^(MSAlertAction *action) {
    NSLog(@"Default action tapped %@", action);
}];
action3.normalColor = [UIColor darkGrayColor];
action3.highlightedColor = [UIColor whiteColor];
[alertController addAction:action3];

[alertController addTextFieldWithConfigurationHandler:nil];

[self presentViewController:alertController animated:YES completion:nil];
18
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
18