按钮用于处理用户操作。它拦截触摸事件并将消息发送到目标对象。
您可以在实用程序区域(窗口的右侧)的属性检查器中的xib中更改按钮属性。
+ (id)buttonWithType:(UIButtonType)buttonType - (UIImage *)backgroundImageForState:(UIControlState)state - (UIImage *)imageForState:(UIControlState)state - (void)setTitle:(NSString *)title forState:(UIControlState)state - (void)addTarget:(id)target action:(SEL)action forControlEvents: (UIControlEvents) controlEvents
-(void)addDifferentTypesOfButton { //使用类方法创建的圆角矩形按钮 UIButton *roundRectButton = [UIButton buttonWithType: UIButtonTypeRoundedRect]; [roundRectButton setFrame:CGRectMake(60, 50, 200, 40)]; //设置按钮的标题 [roundRectButton setTitle:@"Rounded Rect Button" forState: UIControlStateNormal]; [self.view addSubview:roundRectButton]; UIButton *customButton = [UIButton buttonWithType: UIButtonTypeCustom]; [customButton setBackgroundColor: [UIColor lightGrayColor]]; [customButton setTitleColor:[UIColor blackColor] forState: UIControlStateHighlighted]; //设置正常状态的背景图像 [customButton setBackgroundImage:[UIImage imageNamed: @"Button_Default.png"] forState:UIControlStateNormal]; //为突出显示的状态设置背景图像 [customButton setBackgroundImage:[UIImage imageNamed: @"Button_Highlighted.png"] forState:UIControlStateHighlighted]; [customButton setFrame:CGRectMake(60, 100, 200, 40)]; [customButton setTitle:@"Custom Button" forState:UIControlStateNormal]; [self.view addSubview:customButton]; UIButton *detailDisclosureButton = [UIButton buttonWithType: UIButtonTypeDetailDisclosure]; [detailDisclosureButton setFrame:CGRectMake(60, 150, 200, 40)]; [detailDisclosureButton setTitle:@"Detail disclosure" forState: UIControlStateNormal]; [self.view addSubview:detailDisclosureButton]; UIButton *contactButton = [UIButton buttonWithType: UIButtonTypeContactAdd]; [contactButton setFrame:CGRectMake(60, 200, 200, 40)]; [self.view addSubview:contactButton]; UIButton *infoDarkButton = [UIButton buttonWithType: UIButtonTypeInfoDark]; [infoDarkButton setFrame:CGRectMake(60, 250, 200, 40)]; [self.view addSubview:infoDarkButton]; UIButton *infoLightButton = [UIButton buttonWithType: UIButtonTypeInfoLight]; [infoLightButton setFrame:CGRectMake(60, 300, 200, 40)]; [self.view addSubview:infoLightButton]; }
无涯教程必须在项目中添加两个分别名为" Button_Default.png"和" Button_Highlighted.png"的图像,可以通过将图像拖到列出项目文件的导航器区域中来完成
更新ViewController.m中的viewDidLoad,如下所示:
(void)viewDidLoad { [super viewDidLoad]; //创建我们不同类型按钮的自定义方法称为 [self addDifferentTypesOfButton]; //Do any additional setup after loading the view, typically from a nib }
运行应用程序时,将获得以下输出-
链接:https://www.learnfk.comhttps://www.learnfk.com/ios/ios-ui-elements-buttons.html
来源:LearnFk无涯教程网
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)
Tony Bai · Go语言第一课 -〔Tony Bai〕