📜  iOS-UI元素(1)

📅  最后修改于: 2023-12-03 15:15:52.321000             🧑  作者: Mango

iOS UI元素

iOS UI元素是苹果公司为其移动设备iOS平台所设计的用户界面控件和元素。它们是构成iOS应用程序用户界面的基本构件。

本文将介绍常用的iOS UI元素及其描述。

文本元素

iOS平台中的文本元素包括文本标签、文本框、文本视图等,它们用于在屏幕上显示文本内容。

UILabel

UILabel(标签)是iOS应用程序中最常用的文本元素之一。一般情况下,UILabel用于显示静态文本内容。

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
label.text = @"Hello, World!";
[self.view addSubview:label];
UITextField

UITextField(文本框)用于让用户输入单行的文本内容。用户可以在文本框中输入文本内容,也可以从剪贴板中复制/粘贴内容。

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(x, y, width, height)];
textField.placeholder = @"请输入内容";
textField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:textField];
UITextView

UITextView是用于显示多行文本内容的文本视图。

UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(x, y, width, height)];
textView.text = @"这是一段很长的文本...";
[self.view addSubview:textView];
列表元素

iOS应用程序通常需要显示多个数据项,此时列表元素便派上用场,常用的列表元素包括UITableView、UICollectionView等。

UITableView

UITableView用于显示一组数据列表,每个列表项对应一个UITableViewCell。

UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(x, y, width, height) style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
UICollectionView

UICollectionView用于以网格形式展示数据列表。

UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(x, y, width, height) collectionViewLayout:layout];
collectionView.delegate = self;
collectionView.dataSource = self;
[self.view addSubview:collectionView];
交互元素

iOS平台中常用的交互元素包括按钮(UIButton)、开关(UISwitch)等。

UIButton

UIButton用于响应用户的点击事件,可设置不同状态下的显示样式。

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, height)];
[button setTitle:@"点击我" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
UISwitch

UISwitch是一种开关元素,可以用于打开或关闭某些功能。

UISwitch *switchControl = [[UISwitch alloc] initWithFrame:CGRectMake(x, y, width, height)];
switchControl.on = YES;
[switchControl addTarget:self action:@selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:switchControl];