In this tutorial, we’ll be discussing and implementing the UISegmentedControl in our iOS Application.

在本教程中,我们将在iOS应用程序中讨论和实现UISegmentedControl。

UISegmentedControl (UISegmentedControl)

A UISegmentedControl is a horizontal bar which consists of discrete Buttons. These buttons are used to show/hide content or to show different contents when each of the Buttons is clicked.

UISegmentedControl是由不连续的Button组成的水平条。 当单击每个按钮时,这些按钮用于显示/隐藏内容或显示不同的内容。

This way each Button is responsible for a different Segment on the screen.

这样,每个按钮负责屏幕上的不同段。

Following are the helper functions for a UISegmentedControl:

以下是UISegmentedControl的辅助函数:

  • setImage(UIImage?, forSegmentAt: Int): Sets an image at the given position on the segmented control.setImage(UIImage?, forSegmentAt: Int) :在分段控件上的给定位置设置图像。
  • imageForSegment(at: Int) : Returns the image for a specific segmentimageForSegment(at: Int) :返回特定段的图像
  • setTitle(String?, forSegmentAt: Int) : Sets the title at the segment position.setTitle(String?, forSegmentAt: Int) :在段位置设置标题。
  • titleForSegment(at: Int) : Returns the title of the specified segment position.titleForSegment(at: Int) :返回指定段位置的标题。
  • insertSegment(withTitle: String?, at: Int, animated: Bool) : Inserts a segment at a specific position in the UISegmentControl and sets a title.insertSegment(withTitle: String?, at: Int, animated: Bool) :在UISegmentControl中的特定位置插入一个段并设置一个标题。
  • removeAllSegments() : Removes all segment buttons from the UISegmentControlremoveAllSegments() :从UISegmentControl移除所有细分按钮
  • setEnabled(Bool, forSegmentAt: Int) : Enables the segment at the position specified.setEnabled(Bool, forSegmentAt: Int) :在指定位置启用细分。
  • isEnabledForSegment(at: Int) : Returns whether the segment specified is enabled or not.isEnabledForSegment(at: Int) :返回指定段是否启用。
  • setWidth(CGFloat, forSegmentAt: Int) : Sets the width of the specified segment of the Segment Control.setWidth(CGFloat, forSegmentAt: Int) :设置段控件的指定段的宽度。

There are a few properties as well such as tintColor, numberOfSegments.
These are self-explanatory.

还有一些属性,例如tintColornumberOfSegments
这些是不言自明的。

By default, the segments in the UISegment Control have equal width.
Sometimes, a particular Segment in the SegmentedControl can have a longer title. This would squash that title content.

默认情况下,UISegment控件中的段具有相等的宽度。
有时,SegmentedControl中的特定Segment可以具有更长的标题。 这将挤压标题内容。

In order to create segments with different widths, we can either use the setWidth function on each of the segments or use:

为了创建具有不同宽度的线段,我们可以在每个线段上使用setWidth函数,或者使用:

segmentedControl.apportionsSegmentWidthsByContent = true

In the next section, we’ll be creating a new XCode Project with a simple iOS Application that showcases the different use cases of UISegmentedControl.

在下一部分中,我们将使用一个简单的iOS应用程序创建一个新的XCode项目,该项目展示了UISegmentedControl的不同用例。

项目情节提要 (Project Storyboard)

In the right-hand side Attributes Inspector, we can add more segments to the UISegmentedControl.

在右侧的“属性”检查器中,我们可以向UISegmentedControl中添加更多细分。

We have added the IBOutlet of the UISegmentedControl to the ViewController file. The IBAction gets triggered whenever a different Segment in the UISegmentedControl is clicked.

我们已经将UISegmentedControl的IBOutlet添加到ViewController文件中。 每当在UISegmentedControl中单击其他细分时,都会触发IBAction。

As it’s fairly common with other UI Controls in iOS, the valueChanged event gets triggered for the IBAction function to be executed.

由于与iOS中的其他UI控件相当普遍,因此触发了valueChanged事件以执行IBAction函数。

The code for the ViewController.swift class is given below:

下面给出了ViewController.swift类的代码:

import UIKitclass ViewController: UIViewController {@IBOutlet weak var segmentControl1: UISegmentedControl!@IBOutlet weak var segmentedControl2: UISegmentedControl!@IBOutlet weak var labelOne: UILabel!@IBAction func segmentControlAction(_ sender: Any) {let sControl = sender as! UISegmentedControlif sControl.tag == 101{sControl.backgroundColor = UIColor.brownlabelOne.text = sControl.titleForSegment(at: sControl.selectedSegmentIndex)}else{sControl.backgroundColor = UIColor.blackif sControl.selectedSegmentIndex == 0{sControl.tintColor = UIColor.redsControl.insertSegment(withTitle: "New", at: sControl.numberOfSegments-1, animated: true)}else{sControl.tintColor = UIColor.orange}labelOne.text = sControl.titleForSegment(at: sControl.selectedSegmentIndex)}}override func viewDidLoad() {super.viewDidLoad()segmentedControl2.selectedSegmentIndex = 1segmentedControl2.apportionsSegmentWidthsByContent = truesegmentControl1.tag = 101}}

In the above code, each of the UISegmentedConrol is connected to the same IBAction.
We’ve set a tag on one of the controls in the viewDidLoad method.

在上面的代码中,每个UISegmentedConrol连接到相同的IBAction。
我们已经在viewDidLoad方法的其中一个控件上设置了标签。

Inside the IBAction we check for the UISegmentedControl clicked and change the label text and also the tintColor of the UISegmentedControl as shown in the output below

在IBAction内部,我们检查是否单击了UISegmentedControl,并更改了标签文本以及UISegmentedControl的tintColor,如下面的输出所示。

This brings an end to this tutorial. You can download the project from the link below:

本教程到此结束。 您可以从下面的链接下载项目:

iOSUISegmentedControliOSUISegmentedControl

翻译自: https://www.journaldev.com/22872/ios-uisegmentedcontrol

iOS UISegmentedControl相关推荐

  1. iOS UISegmentedControl 的使用

    当用户输入不仅仅是布尔值时,可使用分段控件(UISegmentedControl).分段控件提供一栏按钮(有时称为按钮栏),但只能激活其中一个按钮.分段控件会导致用户在屏幕上看到的内容发生变化.它们常 ...

  2. IOS UISegmentedControl简介

    文章目录 常用属性和方法 使用,页面的切换 AppDelegate.m SubViewController.m颜色随机 MainViewController.m 在许多的应用程序中,开发者会加入一些主 ...

  3. Android常用热门开源库汇总(持续更新)

    原文转载:https://www.yundashi168.com/344.html 请及时关注原文网站,因为后续持续更新都在原网站更新.请多多点赞和关注. 前言 收集了一些比较常见的开源库,特此记录( ...

  4. Android常用开源库整理汇总

    1.基本控件 1.1.TextView ScrollNumber ReadMoreTextView HtmlImage android-autofittextview html-textview Ba ...

  5. Android开源库大全分类汇总(Android技术资料汇总)

    1.基本控件 1.1.TextView ScrollNumber ReadMoreTextView HtmlImage android-autofittextview html-textview Ba ...

  6. 上集: Android开源库大全分类汇总(Android技术资料汇总)

    摘要: 1.基本控件 1.1.TextView ScrollNumber ReadMoreTextView HtmlImage android-autofittextview html-textvie ...

  7. Android开源库大全分类汇总(Android技术资料汇总)...

    1.基本控件 1.1.TextView ScrollNumber ReadMoreTextView HtmlImage android-autofittextview html-textview Ba ...

  8. Android常用开源库种类大全

    ## 1.基本控件 ### [](https://www.yundashi168.com/articles/2018/09/12/1536725366888.html#11textview)1.1.T ...

  9. 2019年最新Android常用开源库汇总上篇(持续更新)

    因为本文资料太多,所以本文分上下篇,并且持续更新,觉得不错的小伙伴们可以关注点赞收藏一下 1.基本控件 1.1.TextView ScrollNumber ReadMoreTextView HtmlI ...

最新文章

  1. 跟我斗图,我用Python爬虫下载几个G的表情砸死你
  2. _捷豹F-pace汽车音响改装黄金声学,中道隔音——哈尔滨小蒋
  3. 强制转换const 引用
  4. atomiclong_想要更快地使用AtomicLong? 等待它。
  5. Qt工作笔记-QPlainTextEdit中数据的获取
  6. Ruby中的Profiling工具
  7. python glob用法_glob模块使用教程
  8. hihoCoder-1097-最小生成树一·Prim算法 (最小生成树)
  9. 使用Pycharm运行TensorFlow,Virtualenv安装TensorFlow
  10. mysql systemctl开机启动_Linux 开机启动项命令:chkconfig 和 systemctl
  11. 考研--线性代数辅导讲义(第一章行列式 第二章矩阵)
  12. H5活动页面抽奖源码
  13. jsp中使用setAttribute发生错误
  14. light动名词_—Thelightintheofficeisstillon.—Oh,Iforgot_____.[ ]A.tur
  15. 通过网线连接两台主机
  16. Zabbix 报告缺少可用的交换空间主机 “Lack of free swap space”问题解决
  17. 打游戏哪种蓝牙耳机比较好?适合玩游戏的无线蓝牙耳机
  18. 【勘误清单】《机器学习》 周志华 北京: 清华大学出版社
  19. 抖音上热门原来这么简单-抖音培训-抖音上热门教程
  20. 联想电脑为什么没有计算机,联想笔记本电脑没有声音怎么办

热门文章

  1. 32位ubuntu 使用pae
  2. 【转】OCaml基础知识
  3. [转载] python自定义error_Python 自定义异常处理Error函数
  4. [转载] Python字符串的截取
  5. 第四节:EasyUI的一些操作
  6. GCC the GNU
  7. 头条号【编编成程】开通
  8. 项目需求:基于微信平台的拼团活动系统
  9. MacOs终端忽略大小写
  10. Android中SlidingDrawer介绍【安卓进化三十四】