iOS开发之自定义弹出框

原文地址:http://blog.sina.com.cn/s/blog_976270ec0100wpnj.html

 

1. 创建一个继承UIView的视图

//

//  DialogView.h

 

#import <UIKit/UIKit.h>

#import <QuartzCore/QuartzCore.h>

@interface DialogView : UIView {

}

-(IBAction)closeClickButton:(id)sender;

@end


//

 //  DialogView.m

#import "DialogView.h"

@implementation DialogView

- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        // Initialization code

        [self setBackgroundColor:[UIColor whiteColor]]; //设置视图背景颜色

        self.layer.cornerRadius = 10;    //设置弹出框为圆角视图

        self.layer.masksToBounds = YES;

        self.layer.borderWidth = 2;   //设置弹出框视图边框宽度

        self.layer.borderColor = [[UIColor colorWithRed:0.50 green:0.10 blue:0.10 alpha:0.5] CGColor];   //设置弹出框边框颜色

        

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

        [button setFrame:CGRectMake(0,0, 23, 23)];

        [button setImage:[UIImage imageNamed:@"close.png"] forState:UIControlStateNormal];    //此按钮用来关闭此视图

        [button addTarget:self action:@selector(closeClickButton:) forControlEvents:UIControlEventTouchUpInside];

        button.tag = 1;

        [self addSubview:button];

    }

    return self;

}

-(IBAction)closeClickButton:(id)sender{

    NSLog(@"关闭视图");

    self.hidden=YES;

}

- (void)dealloc {

    [super dealloc];

}

@end


2.  在UIViewController中的使用

 

......

//定义弹出框大小,并对弹出框进行操作,可添加控件或者视图。

//弹出框视图可以用来显示图片、文字、表格、视频等等,可以将弹出框视图做成多种形式运用于程序中。

- (IBAction)dialogView:(id)sender {

    DialogView *dialogView = [[DialogView alloc] initWithFrame:CGRectMake(0, 0, 200, 300)];

    [self.view addSubview:dialogView];

}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值