Buttons函数

首页 / iOS入门教程 / 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
}

运行应用程序时,将获得以下输出-

iOS Tutorial

这一章《OC - UI元素 - Buttons函数》你学到了什么?在下面做个笔记吧!做站不易,你的分享是对我们最大的支持

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

相关推荐

移动端自动化测试实战 -〔思寒 - 〕

跟月影学可视化 -〔月影 - 〕

Vim 实用技巧必知必会 -〔吴咏炜 - 〕

技术管理案例课 -〔许健 - 〕

.onChange(of: ) 一次用于多个 @State 属性?

从视图模型中更新 EnvironmentObject

如何将单个按钮传递到自定义 ConfirmationDialog

搜索在 ios xamarin.forms 中不起作用

try 在 iOS 中分发 Flutter 应用程序时出现“Invalid Provisioning Profile Signature”错误

如何在不使用 ForEach 的情况下从 SwiftUI 中的列表中删除行分隔符?

视频推荐

15_传智播客iOS视频教程_玩家类的设计 更多视频教程 »