如何修改IASKSpecifierValuesViewController背景/文本颜色及调用自定义类?
Got it, let's break this down into two clear parts: first building your custom IASKSpecifierValuesViewController to adjust its appearance, then making your existing SettingsViewController use this custom class instead of the default one. I'll cover both Storyboard and code-based approaches for you.
1. Create Your Custom IASKSpecifierValuesViewController
Just like how you customized IASKAppSettingsViewController, you'll subclass IASKSpecifierValuesViewController to tweak its background, text colors, and other styles. Here's the code:
import UIKit import InAppSettingsKit class CustomSpecifierValuesViewController: IASKSpecifierValuesViewController { override func viewDidLoad() { super.viewDidLoad() // Set your desired background color view.backgroundColor = .black // Optional: Adjust navigation bar styling if needed navigationController?.navigationBar.tintColor = .white navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white] } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = super.tableView(tableView, cellForRowAt: indexPath) // Customize cell text and background cell.textLabel?.textColor = .white cell.detailTextLabel?.textColor = .gray cell.backgroundColor = .clear cell.selectionStyle = .none return cell } // Optional: Customize section headers if your specifiers use them override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { guard let headerView = view as? UITableViewHeaderFooterView else { return } headerView.textLabel?.textColor = .white headerView.contentView.backgroundColor = .darkGray } }
2. Make Your SettingsViewController Use the Custom Values VC
The key here is overriding a method from IASKAppSettingsViewController that creates the values view controller. This works for both Storyboard and code-based setups.
Option A: Code-Based (Non-Storyboard)
Add this override to your existing SettingsViewController:
override func specifierValuesViewController(for specifier: IASKSpecifier) -> IASKSpecifierValuesViewController { let customVC = CustomSpecifierValuesViewController(style: .plain) customVC.specifier = specifier customVC.settingsStore = settingsStore return customVC }
Option B: Storyboard-Based
- Open your Storyboard, add a new
UITableViewControllerobject. - Set its custom class to
CustomSpecifierValuesViewControllerin the Identity Inspector. - Give it a Storyboard ID (e.g.,
CustomSpecifierValuesVC) in the Identity Inspector. - Add this override to your
SettingsViewController:
override func specifierValuesViewController(for specifier: IASKSpecifier) -> IASKSpecifierValuesViewController { // Replace "Main" with your Storyboard's name if it's different guard let customVC = UIStoryboard(name: "Main", bundle: nil) .instantiateViewController(withIdentifier: "CustomSpecifierValuesVC") as? CustomSpecifierValuesViewController else { // Fall back to the default VC if initialization fails return super.specifierValuesViewController(for: specifier) } customVC.specifier = specifier customVC.settingsStore = settingsStore return customVC }
Now when users tap a settings item that opens a value picker, your styled CustomSpecifierValuesViewController will appear instead of the default one.
内容的提问来源于stack exchange,提问作者Erkam KUCET

