因为我在另一个UINavigationController中需要一个UINavigationController(默认情况下这是不可能的),我创建了一个充当UINavigationController的UIViewController,但是dit不是UINavigationController的子类。
第二个NavigationController(用于子类UINavigationController的那个)呈现(取决于ViewModel的状态)一个控制器。
这是自定义NavigationController:
class OnboardingViewController: UIViewController {
// MARK: - Outlets
@IBOutlet weak var containerView: UIView!
// MARK: - Internal
private var viewModel: OnboardingViewModel = OnboardingViewModel()
// MARK: - View flow
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.isHidden = true
navigateToNextFlow()
}
override var preferredFocusEnvironments: [UIFocusEnvironment] {
switch viewModel.state {
case .recommendations: return recommendationsController.preferredFocusEnvironments
default: return super.preferredFocusEnvironments
}
}
// MARK: - Handlers
var navigateToNextHandler: (() -> Void)?
// MARK: - Controllers
private var recommendationsController: OnboardingRecommendationsViewController {
let controller = UIViewController.instantiate(from: "Onboarding Recommendations") as OnboardingRecommendationsViewController
controller.navigateToNextHandler = { [unowned self] in
self.viewModel.state = .done
self.navigateToNextFlow(animated: true)
}
return controller
}
// MARK: - Navigation
private func navigateToNextFlow(animated: Bool = false) {
switch viewModel.state {
case .recommendations:
add(child: recommendationsController, to: containerView)
case .done:
viewModel.finish()
navigateToNextHandler?()
}
updateFocusIfNeeded()
setNeedsFocusUpdate()
}
}
这是childViewController:
class OnboardingRecommendationsViewController: UIViewController {
// MARK: - Outlets
@IBOutlet weak var onOffButton: UIButton!
@IBOutlet weak var finishButton: UIButton!
// MARK: - Internal
fileprivate let viewModel: OnboardingRecommendationsViewModel = OnboardingRecommendationsViewModel()
// MARK: - View flow
override func viewDidLoad() {
super.viewDidLoad()
setupLabels()
setupOnOffButton()
}
// MARK: - Handlers
var navigateToNextHandler: (() -> Void)?
// MARK: - Focus
override var preferredFocusEnvironments: [UIFocusEnvironment] {
return [finishButton, onOffButton]
}
}
finishButton位于故事板中的onOffButton下方。我正在尝试将最初的焦点设置在finishButton而不是onOffButton上。但是如果他愿意,用户可以关注onOffButton。
无论我尝试什么,它都行不通。调用preferredFocusEnvironments,但按钮的焦点保持错误的顺序。
我做错了什么?
答案 0 :(得分:0)
您尝试过这样吗?
override var preferredFocusEnvironments: [UIFocusEnvironment] {
return [finishButton]
}
或者您可以禁用onOffButton的userInteraction,直到finishButton成为焦点。 (虽然不是一个好的解决方案)
答案 1 :(得分:0)
您应该设置 restoresFocusAfterTransition = false 以避免默认行为。然后,在 preferredFocusEnvironments 中返回要聚焦的视图
答案 2 :(得分:0)
很抱歉,您的回答很晚。原来,我要推送的viewController定义为let
,所以我在自己之上推送了几个实例,这就是为什么preferredFocusEnvironments
似乎不起作用的原因。我实际上看到了具有另一个初始焦点顺序的ViewController的新实例。将viewController的变量声明从let
更改为lazy var
就可以了。因此,最后,它实际上与preferredFocusEnvironments
不起作用无关。但是谢谢您的投入!