gpt4 book ai didi

ios - 以编程方式更新 UISegmentControl 不会更新未选定段的字体颜色

转载 作者:行者123 更新时间:2023-11-28 06:07:59 24 4
gpt4 key购买 nike

我将分段控件用作选项卡栏 Controller 的子选项卡。我在主选项卡上有一个按钮,它应该转换到第二个选项卡(带有子选项卡的那个)并以编程方式将 UISegmentedControl 切换到第一个选项卡(0 索引)。

当然,点击分段控件也能正常工作。但是,当我尝试使用 selectedSegmentIndex 以编程方式更改选项卡时,被取消选择的选项卡不会更新它的字体颜色,这使得标签似乎消失了。

tab bar image

我读过的每篇关于如何以编程方式更改选项卡的帖子都表明我的做法是正确的。谁能告诉我我做错了什么?感谢您的帮助!

代码如下:

class AdvertisingViewController: BaseViewController, TabbedController, ClientSelectorController {

var clientSelectorView: ClientSelector?
var tabControl: UISegmentedControl!
var tabView: UIView!
var nextTabIndex: Int? = nil
var activeTab: AdvertisingTabContent? = nil
var tabs: [AdvertisingTabContent]!
let margin: CGFloat = 20

override func viewDidLoad() {
super.viewDidLoad()

// Initialize the tab view.
tabView = UIView()
view.addSubview(tabView!)

// Initialize the tab controls.
tabControl = UISegmentedControl()
tabControl.insertSegment(withTitle: "Calendar", at: 0, animated: false)
tabControl.insertSegment(withTitle: "TV", at: 1, animated: false)
tabControl.insertSegment(withTitle: "Radio", at: 2, animated: false)
tabControl.insertSegment(withTitle: "Search", at: 3, animated: false)
tabControl.insertSegment(withTitle: "Display", at: 4, animated: false)
tabControl.selectedSegmentIndex = 0
tabControl.addTarget(self, action: #selector(selectedTab(sender:)), for: .valueChanged)
view.addSubview(tabControl!)

// Tab control styles.
let font = UIFont.systemFont(ofSize: 12)
tabControl.setTitleTextAttributes([
NSAttributedStringKey.font: font,
NSAttributedStringKey.foregroundColor: Color.lightGrey
], for: .normal)
tabControl.removeBorders()

// Intialize the tabs.
nextTabIndex = nil
tabs = [
CampaignsTab(view: tabView),
TVTab(view: tabView),
RadioTab(view: tabView),
PaidSearchTab(view: tabView),
DisplayTab(view: tabView),
]

// Bind clientSelector button to clientSelectButtonPressed method.
initializeClientSelector()

// Set the background color.
tabView.backgroundColor = Color.backgroundColor
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

// Update client selector title.
layoutClientSelector()

// Update tab view & segmented control
let controlHeight: CGFloat = 35
tabControl.frame = CGRect(x: margin, y: clientSelectorView!.frame.height, width: view.frame.width - (margin * 2), height: controlHeight)

// Update the tab view.
tabView.frame = CGRect(x: 0, y: clientSelectorView!.frame.height + controlHeight + margin, width: view.frame.width, height: view.frame.height - clientSelectorView!.frame.height - controlHeight - margin)

// Build tab.
buildTab()
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

// Update the tabControl.
if let next = nextTabIndex {
if next != tabControl.selectedSegmentIndex {
tabControl.selectedSegmentIndex = next
}
nextTabIndex = nil
}
}

func clientSelectorClicked(selectorModal: UIViewController) {
present(selectorModal, animated: true, completion: nil)
}

// MARK: - Tabs Control

/**
The user selected a different tab.
*/
@objc func selectedTab(sender: UIButton) {
buildTab()
}

/**
A tab was selected from another part of the app.
(This is called before viewWillAppear from another tab)
*/
func switchToTab(atIndex index: Int) {
nextTabIndex = index
}

func buildTab() {

// Clean up the old tab.
activeTab?.cleanUp()
activeTab = nil

// Check next index.
var index = tabControl.selectedSegmentIndex
if let next = nextTabIndex {
index = next
}

// Add/Build the new tab.
if index >= 0 && tabs.count > index {
activeTab = tabs[index]
activeTab?.buildViews()
}
}
}

更新:

今天早上又花了几个小时寻找解决方案。我还尝试通过在分段控件的 subview 中找到标签并手动更改字体颜色来破解它。我可以更改标签的文本,但不能更改字体颜色。有什么东西压倒了它。

是否有其他方法可以通过编程方式更改所选索引?我需要提交一张票给苹果吗?没有其他人有这个问题吗?提前感谢您提供的任何帮助。

最佳答案

经过一番折腾,我终于找到了答案。我正在使用以下帖子中的方法删除边框:

Swift: How to remove border from segmented control

extension UISegmentedControl {
func applyAppStyles() {
setBackgroundImage(imageWithColor(color: Color.contentBackgroundColor), for: .normal, barMetrics: .default)
setBackgroundImage(imageWithColor(color: Color.orange), for: .selected, barMetrics: .default)
setDividerImage(imageWithColor(color: UIColor.clear), forLeftSegmentState: .normal, rightSegmentState: .normal, barMetrics: .default)
}

// create a 1x1 image with this color
private func imageWithColor(color: UIColor) -> UIImage {
let rect = CGRect(x: 0.0, y: 0.0, width: 1.0, height: 1.0)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
context!.setFillColor(color.cgColor);
context!.fill(rect);
let image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image!
}
}

我对 setBackgroundImage 的第一次调用导致了这个问题;我不确定为什么。但是下面的代码修复了它:

tabControl.setTitleTextAttributes([
NSAttributedStringKey.foregroundColor: Color.darkGreyBlue
], for: .selected)

关于ios - 以编程方式更新 UISegmentControl 不会更新未选定段的字体颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47636999/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com