タブ付きコントローラのサブタブとしてセグメント化されたコントロールを使用しています。 2番目のタブ(サブタブ付きのもの)に移行し、プログラムでUISegmentedControlを最初のタブ(0インデックス)に切り替えるはずのメインタブにボタンがあります。UISegmentControlをプログラムで更新すると、選択されていないセグメントのフォントの色が更新されない
もちろん、セグメント化されたコントロールをタップしても問題ありません。しかし、selectedSegmentIndex
を使用してタブをプログラムで変更しようとすると、タブの選択が解除されてもフォントの色が更新されず、ラベルが消えるように見えます。
私はプログラムでタブを変更する方法について読んだすべてのポストは、私はそれを正しい方法でやっていることを示しています。誰かが私が間違っていることを教えてもらえますか?ご協力ありがとうございました!今朝解決策を探してカップルより多くの時間を過ごし
:
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()
}
}
}
アップデート:ここで
がコードです。私はまた、セグメント化されたコントロールのサブビューでラベルを見つけて、手動でフォントの色を変更して、ハッキングを試みました。ラベルのテキストは変更できますが、フォントの色は変更できません。何かがそれをオーバーライドしています。
プログラムによって選択されたインデックスを変更する他の方法はありますか?りんごに切符を提出する必要がありますか?誰もこの問題を抱えていませんか?あなたが提供できる任意のヘルプに事前に感謝します。