OC 中的 Buttons函数

首页 / iOS入门教程 / OC 中的 Buttons函数

按钮用于处理用户操作。它拦截触摸事件并将消息发送到目标对象。

iOS Tutorial

Buttons - 属性

您可以在实用程序区域(窗口的右侧)的属性检查器中的xib中更改按钮属性。

iOS Tutorial

Buttons - 类型

  • UIButtonTypeCustom
  • UIButtonTypeRoundedRect
  • UIButtonTypeDetailDisclosure
  • UIButtonTypeInfoLight
  • UIButtonTypeInfoDark
  • UIButtonTypeContactAdd

Buttons - 重要属性

  • imageView
  • titleLabel

Buttons - 重要方法

+ (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

Buttons - 自定义方法

-(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无涯教程网

iOS Tutorial

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

设计模式之美 -〔王争〕

正则表达式入门课 -〔涂伟忠〕

说透区块链 -〔自游〕

Tony Bai · Go语言第一课 -〔Tony Bai〕

自动化测试高手课 -〔柳胜〕

云原生架构与GitOps实战 -〔王炜〕

大型Android系统重构实战 -〔黄俊彬〕

结构思考力 · 透过结构看表达 -〔李忠秋〕

互联网人的数字化企业生存指南 -〔沈欣〕

好记忆不如烂笔头。留下您的足迹吧 :)