iOS开发——UI基础-按钮的创建和设置
2015-07-15 02:52
441 查看
@interface ViewController () - (IBAction)customBtnClick; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; // 1.设置标题 // btn.titleLabel.text = @"我是按钮"; // 千万不要这样写 // 一般情况下给按钮设置内容都是setXXX [btn setTitle:@"我是按钮" forState:UIControlStateNormal]; [btn setTitle:@"哥是高亮" forState:UIControlStateHighlighted]; // 2.设置标题颜色 // btn.backgroundColor = [UIColor redColor]; [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; // 3.设置图标 [btn setImage:[UIImage imageNamed:@"common_icon_check"] forState:UIControlStateNormal]; // 4.设置背景图片 [btn setBackgroundImage:[UIImage imageNamed:@"common_button_big_blue_highlighted"] forState:UIControlStateNormal]; // 5.监听按钮的点击 // Target:让谁监听按钮 // action:监听到之后需要执行的方法 // Events:事件的类型 // 规律: 只要是继承于UIControl的控件, 都可以通过addTarget来添加监听 [btn addTarget:self action:@selector(customBtnClick) forControlEvents:UIControlEventTouchUpInside]; // UISwitch sw = nil; // [sw addTarget:<#(nullable id)#> action:<#(nonnull SEL)#> forControlEvents:<#(UIControlEvents)#>] // UISegmentedControl *sc = nil; // [sc addTarget:<#(nullable id)#> action:<#(nonnull SEL)#> forControlEvents:<#(UIControlEvents)#>] // UITextField *tf = nil; // [tf addTarget:<#(nullable id)#> action:<#(nonnull SEL)#> forControlEvents:<#(UIControlEvents)#>] btn.titleLabel.font = [UIFont systemFontOfSize:14]; btn.frame = CGRectMake(100, 100, 100, 100); [self.view addSubview:btn]; } - (IBAction)customBtnClick { NSLog(@"%s", __func__); } @end
相关文章推荐
- iOS开发——UI基础-Xcode资源拷贝
- iOS开发——UI基础-UIImage,UIImageView的使用
- “黑马程序员” GUI 图形用户界面
- iOS开发——UI基础-UIButton、UIImageView、UILabel的选择
- RequireJS示例
- mysql]ERROR 1364 (HY000): Field 'ssl_cipher' doesn't have a default value
- iOS学习笔记-02-SubUIViews
- ios学习笔记-01-UIView
- iOS开发UI篇 -- 0401微博主页-自定义cell
- arduino uno 多机串口通信
- iOS多线程编程之NSOperation和NSOperationQueue的使用
- 欢迎使用CSDN-markdown编辑器
- UITableVIew的性能优化-重用原理
- [翻译] BAFluidView
- [Erlang_Question32]ibrowse流程及性能测试
- iOS Xcode 6报错:setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key v
- ueditor引入通用优酷视频
- Easyui Tab刷新
- UIScrollView 实现比例缩放
- UiAutomator喷射事件的源代码分析