- (IBAction)top:(UIButton *)sender {ide
CGRect btnFrame = self.headBtn.frame;佈局
btnFrame.origin.y -= 10;code
self.headBtn.frame = btnFrame;orm
}對象
self.headBtn.frame.origin.y -= 10;blog
// 1.建立一個自定義的按鈕圖片
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];資源
// 2.添加按鈕開發
[self.view addSubview:btn];get
// 3.設置按鈕的位置和尺寸
btn.frame = CGRectMake(100, 100, 100, 100);
// 4.監聽按鈕點擊(點擊按鈕後就會調用self的btnClick方法)
[btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
// 5.設置按鈕在默認狀態下的屬性
// 5.1.默認狀態的背景
[btn setBackgroundImage:[UIImage imageNamed:@"btn_01"] forState:UIControlStateNormal];
// 5.2.默認狀態的文字
[btn setTitle:@"點我啊" forState:UIControlStateNormal];
// 5.3.默認狀態的文字顏色
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
// 6.設置按鈕在高亮狀態下的屬性
// 6.1.高亮狀態的背景
[btn setBackgroundImage:[UIImage imageNamed:@"btn_02"] forState:UIControlStateHighlighted];
// 6.2.高亮狀態的文字
[btn setTitle:@"摸我幹啥" forState:UIControlStateHighlighted];
// 6.3.高亮狀態的文字顏色
[btn setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
// 建立一個自定義的按鈕
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
// 默認狀態的背景
[btn setBackgroundImage:[UIImage imageNamed:@"btn_01"] forState:UIControlStateNormal];
// 默認狀態的文字
[btn setTitle:@"點我啊" forState:UIControlStateNormal];
// 默認狀態的文字顏色
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];