Ios 自定义分段控件还是。。?
推特截图 我想要Swift 4中的这种分段控件。我已经研究过苹果的文档,但是我找不到我需要的东西,比如删除边框、删除cornerRadius等等。我如何定制像Twitter这样的分段控件?或者是否有其他选项卡/细分解决方案?谢谢。您可以使用部分。它有你想要的TabBar。但是有必要进行一些代码分析 有课。有一个属性UIImageView指示符,它具有淡色背景(),可以在段字符串的上方或底部绘制。还有一个调整线段大小的函数。还有一个变量可以帮助绘制和控制此图形 我不确定,但您可以简单地将此类包含到您的项目中,甚至在swift中使用它。您可以使用其中的一部分。它有你想要的TabBar。但是有必要进行一些代码分析 有课。有一个属性UIImageView指示符,它具有淡色背景(),可以在段字符串的上方或底部绘制。还有一个调整线段大小的函数。还有一个变量可以帮助绘制和控制此图形Ios 自定义分段控件还是。。?,ios,swift,controls,Ios,Swift,Controls,推特截图 我想要Swift 4中的这种分段控件。我已经研究过苹果的文档,但是我找不到我需要的东西,比如删除边框、删除cornerRadius等等。我如何定制像Twitter这样的分段控件?或者是否有其他选项卡/细分解决方案?谢谢。您可以使用部分。它有你想要的TabBar。但是有必要进行一些代码分析 有课。有一个属性UIImageView指示符,它具有淡色背景(),可以在段字符串的上方或底部绘制。还有一个调整线段大小的函数。还有一个变量可以帮助绘制和控制此图形 我不确定,但您可以简单地将此类包含
我不确定,但您可以简单地将此类包含到项目中,甚至在swift中使用它。以下是如何自定义UISegmentedControl以在选中时显示底部边框:
- 为分段控件创建一个容器视图,并使用自动布局将其固定到其超级视图李>
- 将分段控件作为子视图添加到容器视图中,并使用“自动布局”将其固定到容器视图的边缘
- 创建底部下划线视图,将其作为子视图添加到容器视图中,并对其应用自动布局(请参见示例实现)李>
- 然后设置一个事件侦听器:在分段控件的值更改事件上,更改底部下划线视图的原点,以便将其移动到选定分段的下方
- 我还添加了一些关于如何格式化分段控件标签的字体和文本颜色的代码,请参见下面的示例
import UIKit
class ViewController: UIViewController {
private enum Constants {
static let segmentedControlHeight: CGFloat = 40
static let underlineViewColor: UIColor = .blue
static let underlineViewHeight: CGFloat = 2
}
// Container view of the segmented control
private lazy var segmentedControlContainerView: UIView = {
let containerView = UIView()
containerView.backgroundColor = .clear
containerView.translatesAutoresizingMaskIntoConstraints = false
return containerView
}()
// Customised segmented control
private lazy var segmentedControl: UISegmentedControl = {
let segmentedControl = UISegmentedControl()
// Remove background and divider colors
segmentedControl.backgroundColor = .clear
segmentedControl.tintColor = .clear
// Append segments
segmentedControl.insertSegment(withTitle: "First", at: 0, animated: true)
segmentedControl.insertSegment(withTitle: "Second", at: 1, animated: true)
segmentedControl.insertSegment(withTitle: "Third", at: 2, animated: true)
// Select first segment by default
segmentedControl.selectedSegmentIndex = 0
// Change text color and the font of the NOT selected (normal) segment
segmentedControl.setTitleTextAttributes([
NSAttributedStringKey.foregroundColor: UIColor.black,
NSAttributedStringKey.font: UIFont.systemFont(ofSize: 16, weight: .regular)], for: .normal)
// Change text color and the font of the selected segment
segmentedControl.setTitleTextAttributes([
NSAttributedStringKey.foregroundColor: UIColor.blue,
NSAttributedStringKey.font: UIFont.systemFont(ofSize: 16, weight: .bold)], for: .selected)
// Set up event handler to get notified when the selected segment changes
segmentedControl.addTarget(self, action: #selector(segmentedControlValueChanged), for: .valueChanged)
// Return false because we will set the constraints with Auto Layout
segmentedControl.translatesAutoresizingMaskIntoConstraints = false
return segmentedControl
}()
// The underline view below the segmented control
private lazy var bottomUnderlineView: UIView = {
let underlineView = UIView()
underlineView.backgroundColor = Constants.underlineViewColor
underlineView.translatesAutoresizingMaskIntoConstraints = false
return underlineView
}()
private lazy var leadingDistanceConstraint: NSLayoutConstraint = {
return bottomUnderlineView.leftAnchor.constraint(equalTo: segmentedControl.leftAnchor)
}()
override func viewDidLoad() {
super.viewDidLoad()
// Add subviews to the view hierarchy
// (both segmentedControl and bottomUnderlineView are subviews of the segmentedControlContainerView)
view.addSubview(segmentedControlContainerView)
segmentedControlContainerView.addSubview(segmentedControl)
segmentedControlContainerView.addSubview(bottomUnderlineView)
// Constrain the container view to the view controller
let safeLayoutGuide = self.view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
segmentedControlContainerView.topAnchor.constraint(equalTo: safeLayoutGuide.topAnchor),
segmentedControlContainerView.leadingAnchor.constraint(equalTo: safeLayoutGuide.leadingAnchor),
segmentedControlContainerView.widthAnchor.constraint(equalTo: safeLayoutGuide.widthAnchor),
segmentedControlContainerView.heightAnchor.constraint(equalToConstant: Constants.segmentedControlHeight)
])
// Constrain the segmented control to the container view
NSLayoutConstraint.activate([
segmentedControl.topAnchor.constraint(equalTo: segmentedControlContainerView.topAnchor),
segmentedControl.leadingAnchor.constraint(equalTo: segmentedControlContainerView.leadingAnchor),
segmentedControl.centerXAnchor.constraint(equalTo: segmentedControlContainerView.centerXAnchor),
segmentedControl.centerYAnchor.constraint(equalTo: segmentedControlContainerView.centerYAnchor)
])
// Constrain the underline view relative to the segmented control
NSLayoutConstraint.activate([
bottomUnderlineView.bottomAnchor.constraint(equalTo: segmentedControl.bottomAnchor),
bottomUnderlineView.heightAnchor.constraint(equalToConstant: Constants.underlineViewHeight),
leadingDistanceConstraint,
bottomUnderlineView.widthAnchor.constraint(equalTo: segmentedControl.widthAnchor, multiplier: 1 / CGFloat(segmentedControl.numberOfSegments))
])
}
@objc private func segmentedControlValueChanged(_ sender: UISegmentedControl) {
changeSegmentedControlLinePosition()
}
// Change position of the underline
private func changeSegmentedControlLinePosition() {
let segmentIndex = CGFloat(segmentedControl.selectedSegmentIndex)
let segmentWidth = segmentedControl.frame.width / CGFloat(segmentedControl.numberOfSegments)
let leadingDistance = segmentWidth * segmentIndex
UIView.animate(withDuration: 0.3, animations: { [weak self] in
self?.leadingDistanceConstraint.constant = leadingDistance
self?.layoutIfNeeded()
})
}
}
以下是如何自定义UISegmentedControl以在选中它时显示底部边框:
- 为分段控件创建一个容器视图,并使用自动布局将其固定到其超级视图李>
- 将分段控件作为子视图添加到容器视图中,并使用“自动布局”将其固定到容器视图的边缘
- 创建底部下划线视图,将其作为子视图添加到容器视图中,并对其应用自动布局(请参见示例实现)李>
- 然后设置一个事件侦听器:在分段控件的值更改事件上,更改底部下划线视图的原点,以便将其移动到选定分段的下方
- 我还添加了一些关于如何格式化分段控件标签的字体和文本颜色的代码,请参见下面的示例
import UIKit
class ViewController: UIViewController {
private enum Constants {
static let segmentedControlHeight: CGFloat = 40
static let underlineViewColor: UIColor = .blue
static let underlineViewHeight: CGFloat = 2
}
// Container view of the segmented control
private lazy var segmentedControlContainerView: UIView = {
let containerView = UIView()
containerView.backgroundColor = .clear
containerView.translatesAutoresizingMaskIntoConstraints = false
return containerView
}()
// Customised segmented control
private lazy var segmentedControl: UISegmentedControl = {
let segmentedControl = UISegmentedControl()
// Remove background and divider colors
segmentedControl.backgroundColor = .clear
segmentedControl.tintColor = .clear
// Append segments
segmentedControl.insertSegment(withTitle: "First", at: 0, animated: true)
segmentedControl.insertSegment(withTitle: "Second", at: 1, animated: true)
segmentedControl.insertSegment(withTitle: "Third", at: 2, animated: true)
// Select first segment by default
segmentedControl.selectedSegmentIndex = 0
// Change text color and the font of the NOT selected (normal) segment
segmentedControl.setTitleTextAttributes([
NSAttributedStringKey.foregroundColor: UIColor.black,
NSAttributedStringKey.font: UIFont.systemFont(ofSize: 16, weight: .regular)], for: .normal)
// Change text color and the font of the selected segment
segmentedControl.setTitleTextAttributes([
NSAttributedStringKey.foregroundColor: UIColor.blue,
NSAttributedStringKey.font: UIFont.systemFont(ofSize: 16, weight: .bold)], for: .selected)
// Set up event handler to get notified when the selected segment changes
segmentedControl.addTarget(self, action: #selector(segmentedControlValueChanged), for: .valueChanged)
// Return false because we will set the constraints with Auto Layout
segmentedControl.translatesAutoresizingMaskIntoConstraints = false
return segmentedControl
}()
// The underline view below the segmented control
private lazy var bottomUnderlineView: UIView = {
let underlineView = UIView()
underlineView.backgroundColor = Constants.underlineViewColor
underlineView.translatesAutoresizingMaskIntoConstraints = false
return underlineView
}()
private lazy var leadingDistanceConstraint: NSLayoutConstraint = {
return bottomUnderlineView.leftAnchor.constraint(equalTo: segmentedControl.leftAnchor)
}()
override func viewDidLoad() {
super.viewDidLoad()
// Add subviews to the view hierarchy
// (both segmentedControl and bottomUnderlineView are subviews of the segmentedControlContainerView)
view.addSubview(segmentedControlContainerView)
segmentedControlContainerView.addSubview(segmentedControl)
segmentedControlContainerView.addSubview(bottomUnderlineView)
// Constrain the container view to the view controller
let safeLayoutGuide = self.view.safeAreaLayoutGuide
NSLayoutConstraint.activate([
segmentedControlContainerView.topAnchor.constraint(equalTo: safeLayoutGuide.topAnchor),
segmentedControlContainerView.leadingAnchor.constraint(equalTo: safeLayoutGuide.leadingAnchor),
segmentedControlContainerView.widthAnchor.constraint(equalTo: safeLayoutGuide.widthAnchor),
segmentedControlContainerView.heightAnchor.constraint(equalToConstant: Constants.segmentedControlHeight)
])
// Constrain the segmented control to the container view
NSLayoutConstraint.activate([
segmentedControl.topAnchor.constraint(equalTo: segmentedControlContainerView.topAnchor),
segmentedControl.leadingAnchor.constraint(equalTo: segmentedControlContainerView.leadingAnchor),
segmentedControl.centerXAnchor.constraint(equalTo: segmentedControlContainerView.centerXAnchor),
segmentedControl.centerYAnchor.constraint(equalTo: segmentedControlContainerView.centerYAnchor)
])
// Constrain the underline view relative to the segmented control
NSLayoutConstraint.activate([
bottomUnderlineView.bottomAnchor.constraint(equalTo: segmentedControl.bottomAnchor),
bottomUnderlineView.heightAnchor.constraint(equalToConstant: Constants.underlineViewHeight),
leadingDistanceConstraint,
bottomUnderlineView.widthAnchor.constraint(equalTo: segmentedControl.widthAnchor, multiplier: 1 / CGFloat(segmentedControl.numberOfSegments))
])
}
@objc private func segmentedControlValueChanged(_ sender: UISegmentedControl) {
changeSegmentedControlLinePosition()
}
// Change position of the underline
private func changeSegmentedControlLinePosition() {
let segmentIndex = CGFloat(segmentedControl.selectedSegmentIndex)
let segmentWidth = segmentedControl.frame.width / CGFloat(segmentedControl.numberOfSegments)
let leadingDistance = segmentWidth * segmentIndex
UIView.animate(withDuration: 0.3, animations: { [weak self] in
self?.leadingDistanceConstraint.constant = leadingDistance
self?.layoutIfNeeded()
})
}
}
很抱歉网间网操作系统。更多细节?对不起。网间网操作系统。更多细节?嗯,看起来很漂亮。多谢各位!除非我能用,你能帮我吗?嗯,看起来很漂亮。多谢各位!除非我能使用,你能帮我吗?在iOS 13上我不能使线条动画化,有没有办法解决这个问题?在iOS 13上我不能使线条动画化,有没有办法解决这个问题?