• iOS开发-按钮的基本使用


    //

    //  ViewController.m

    //  05-用代码创建按钮

    //

    //  Created by vic fan on 2017/7/30.

    //  Copyright © 2017 李洪强. All rights reserved.

    //

     

    #import "ViewController.h"

     

    @interface ViewController ()

     

    @end

     

    @implementation ViewController

    //视图加载完毕后调用,一般用来初始化(添加)控件,这个方法是系统主动调用的

    //

    - (void)viewDidLoad {

        [super viewDidLoad];

        //1.创建一个按钮对象

        UIButton *headBtn = [[UIButton alloc]init];

        //2.设置按钮的frame

        headBtn.frame = CGRectMake(30, 30, 120, 120);

        //3.添加到视图上

        [self.view addSubview:headBtn];

        //4.设置图片

        //4.1 设置普通状态的图片

        UIImage *nomalImage = [UIImage imageNamed:@"btn_01"];

        [headBtn setBackgroundImage:nomalImage forState:UIControlStateNormal];

        UIImage *hightImage = [UIImage imageNamed:@"btn_02"];

        [headBtn setBackgroundImage:hightImage forState:UIControlStateHighlighted];

        //5.设置文字

        //5.1 设置普通状态下的文字

        [headBtn setTitle:@"摸我吧" forState:UIControlStateNormal];

        //设置高亮状态的文字

        [headBtn setTitle:@"摸我干啥" forState:UIControlStateHighlighted];

        //6.设置文字颜色

        [headBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

        //设置高亮状态文字颜色

        [headBtn setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];

        //7 添加按钮的点击事件

    //    forControlEvents  监听的是什么事件

        [headBtn addTarget:self action:@selector(benClicked) forControlEvents:UIControlEventTouchUpInside];

        

    }

     

    - (void)benClicked{

        NSLog(@"我们快下课了");

        

    }

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

     

     

    @end

  • 相关阅读:
    [C++] 用Xcode来写C++程序[5] 函数的重载与模板
    【转】字符编码详解——彻底理解掌握编码知识,“乱码”不复存在
    【转】无法将notepad++添加到打开方式列表中的解决办法
    【转】关于启用 HTTPS 的一些经验分享
    【转】GPU 与CPU的作用协调,工作流程、GPU整合到CPU得好处
    【转】excel 末尾是0 恢复数据方法
    【转】怎么让VS2015编写的程序在XP中顺利运行
    【转】深入 Docker:容器和镜像
    【转】SSL/TLS协议运行机制的概述
    【转】C++怎么读写windows剪贴板的内容?比如说自动把一个字符串复制.
  • 原文地址:https://www.cnblogs.com/LiLihongqiang/p/7259437.html
Copyright © 2020-2023  润新知