// 创建按钮
UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// 设置按钮标题
[button setTitle:@"触摸我!" forState:UIControlStateNormal];
// 根据标题长度自动决定按钮尺寸
[button sizeToFit];
// 将按钮布置在中心位置
button.center = self.view.center;
// 画面变化时按钮位置自动调整
button.autoresizingMask = UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight |
UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleBottomMargin;
// 设置按钮被触摸时响应方法
[button addTarget:self
action:@selector(buttonDidPush:)
forControlEvents:UIControlEventTouchUpInside];
时间: 2024-10-10 21:27:11