iOS開發-ios7 UIAlertView自定義 – iPhone手機開發技術文章 iPhone軟體開發教學課程

之前一個項目適配ios7之後,發現原先的UIAlertView無法正常顯示。

後來發現ios7裡面原生態的UIAlertView不支持自定義瞭。

然後就去github上找瞭下。發現瞭一個不錯的第三方庫。和我們之前的使用習慣差不多。

mark一下。

下面給個簡單的示例。

1.導入文件。

將這兩個文件加入我們的工程下。vcD4KPHA+PGJyPgo8L3A+CjxoMT4yLsztvNPNt87EvP48L2gxPgo8cD7U2tDo0qrKudPDVUlBbGVydFZpZXe1xLXYt70szO280823zsS8/qGjPC9wPgo8cD4jaW1wb3J0IA==”CustomIOS7AlertView.h”

並且添加協議。

比如我的.h文件

#import 
#import "CustomIOS7AlertView.h"

@interface ViewController : UIViewController

@end

3.添加UIAlertView

先給出.m文件。

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Just a subtle background color
    [self.view setBackgroundColor:[UIColor colorWithRed:0.8f green:0.8f blue:0.8f alpha:1.0f]];

    // A simple button to launch the demo
    UIButton *launchDialog = [UIButton buttonWithType:UIButtonTypeCustom];
    [launchDialog setFrame:CGRectMake(10, 30, self.view.bounds.size.width-20, 50)];
    [launchDialog addTarget:self action:@selector(launchDialog:) forControlEvents:UIControlEventTouchDown];
    [launchDialog setTitle:@"Launch Dialog" forState:UIControlStateNormal];
    [launchDialog setBackgroundColor:[UIColor whiteColor]];
    [launchDialog setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [launchDialog.layer setBorderWidth:0];
    [launchDialog.layer setCornerRadius:5];
    [self.view addSubview:launchDialog];
}

- (IBAction)launchDialog:(id)sender
{
    // Here we need to pass a full frame
    CustomIOS7AlertView *alertView = [[CustomIOS7AlertView alloc] init];

    // Add some custom content to the alert view
    [alertView setContainerView:[self createDemoView]];

    // Modify the parameters
    [alertView setButtonTitles:[NSMutableArray arrayWithObjects:@"Close1", @"Close2", @"Close3", nil]];
    [alertView setDelegate:self];
    
    // You may use a Block, rather than a delegate.
    [alertView setOnButtonTouchUpInside:^(CustomIOS7AlertView *alertView, int buttonIndex) {
        NSLog(@"Block: Button at position %d is clicked on alertView %d.", buttonIndex, [alertView tag]);
        [alertView close];
    }];
    
    [alertView setUseMotionEffects:true];

    // And launch the dialog
    [alertView show];
}

- (void)customIOS7dialogButtonTouchUpInside: (CustomIOS7AlertView *)alertView clickedButtonAtIndex: (NSInteger)buttonIndex
{
    NSLog(@"Delegate: Button at position %d is clicked on alertView %d.", buttonIndex, [alertView tag]);
    [alertView close];
}

- (UIView *)createDemoView
{
    UIView *demoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 290, 200)];

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 270, 180)];
    [imageView setImage:[UIImage imageNamed:@"demo"]];
    [demoView addSubview:imageView];

    return demoView;
}

@end

這裡,通過一個button綁定彈出UIAlertView。 當然,你也可以在其他條件觸發launchDialog方法。

然後UIAlertView載入自己定義的視圖(createDemoView)。

另外,塊內的setOnButtonTouchUpInside 和 單獨的delegate customIOS7dialogButtonTouchUpInside方法都能響應我們的選擇。

具體選擇哪種方式因人而異。

下面給出例子程序下載鏈接:https://download.csdn.net/detail/hitwhylz/6857765

學習的路上,與君共勉。

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *