1 //打开 ButtonViewController.m 文件: 2 3 - (void)viewDidLoad { 4 5 [super viewDidLoad]; 6 7 //创建按钮 8 UIButton *sampleButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 9 10 11 //设置按钮位置 12 [sampleButton setFrame:CGRectMake(10, 100, self.view.bounds.size.width- 20, 52)]; 13 14 //定义按钮标题 15 [sampleButton setTitle:@"Button Title" forState:UIControlStateNormal]; 16 17 //定义按钮标题字体格式 18 [sampleButton.titleLabel setFont:[UIFont boldSystemFontOfSize:20]]; 19 20 //定义按钮背景图片,redButton.png已经存在,拖放添加图片文件到image项目文件夹中 21 [sampleButton setBackgroundImage:[[UIImage imageNamed:@"redButton.png"]stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0] forState:UIControlStateNormal]; 22 23 //添加点击按钮所执行的程式 24 [sampleButton addTarget:self action:@selector(buttonClicked)forControlEvents:UIControlEventTouchUpInside]; 25 26 //在 View 中加入按钮 27 [self.viewaddSubview:sampleButton]; 28 29 30 } 31 32 // 33 34 -(void) buttonClicked { 35 36 }
相关文章: