iOS

用代码创建View

2019-04-14  本文已影响0人  习惯了_就好
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //创建Button
    UIButton * button = [[UIButton alloc] init];
    //设置Button的位置和大小
    button.frame = CGRectMake(100, 100, 100, 100);
    //设置Button正常状态下的文字
    [button setTitle:@"点我呀" forState:UIControlStateNormal];
    //设置Button按下状态的文字
    [button setTitle:@"点我干嘛" forState:UIControlStateHighlighted];
    //设置Button正常状态下文字颜色
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    //设置Button按下时的文字颜色
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
    //设置Button正常状态下的背景图片
    [button setBackgroundImage:[UIImage imageNamed:@"btn_01"] forState:UIControlStateNormal];
    //设置Button按下状态时的背景图片
    [button setBackgroundImage:[UIImage imageNamed:@"btn_02"] forState:UIControlStateHighlighted];
    //给Button设置监听
    [button addTarget:self action:@selector(onItemClickListener:) forControlEvents:UIControlEventTouchUpInside];
    //将tButton添加到控制器中
    [self.view addSubview:button];

上一篇下一篇

猜你喜欢

热点阅读