效果圖以下:ide
下邊貼上代碼:orm
ViewController.m事件
- (void)viewDidLoad {get
[super viewDidLoad];it
self.view.backgroundColor = [UIColor yellowColor];io
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];select
[btn setTitle:@"點擊打開透明頁" forState:UIControlStateNormal];float
btn.backgroundColor = [UIColor redColor];im
[btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];next
[self.view addSubview:btn];
}
-(void)btnClicked:(UIButton*)sender{
NSLog(@"你點擊了btn");
//nextvc
UIViewController *nextVC = [[UIViewController alloc]init];
nextVC.view.backgroundColor = [UIColor clearColor];
//設置上邊的半透明的背景view
UIButton *bgBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 300)];
bgBtn.backgroundColor = [UIColor blackColor];
//設置形態的方式
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0){
nextVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
}else{
nextVC.modalPresentationStyle = UIModalPresentationCurrentContext;
}
bgBtn.alpha = 0.8;
//天機點擊事件 用來驗證可點擊
[bgBtn addTarget:self action:@selector(btnClicked2:) forControlEvents:UIControlEventTouchUpInside];
[nextVC.view addSubview:bgBtn];
//分享的按鈕的設置
UIButton *shareBtn = [[UIButton alloc]initWithFrame:CGRectMake(50, 350, 50, 50)];
[nextVC.view addSubview:shareBtn];
shareBtn.backgroundColor = [UIColor purpleColor];
[shareBtn setTitle:@"tittle1" forState:UIControlStateNormal];
[shareBtn addTarget:self action:@selector(tittleClicked:) forControlEvents:UIControlEventTouchUpInside];
[self presentViewController:nextVC animated:YES completion:nil];
}
-(void)tittleClicked:(UIButton *)sender
{
NSLog(@"你點擊了第一個tittle");
}
-(void)btnClicked2:(UIButton*)sender
{
NSLog(@"點擊的是next的bgBtn");
}