UINavigationBar 的 UIBindingObserver 无法按预期工作
UIBindingObserver for UINavigationBar does not work as expected
我为 barTintColor
新的 UIColor
和 title
绑定到 UIBindingObserver
,但它没有显示。有线的事情是,当我将 UIViewController 拖回并释放时,一切都会出现
代码
extension Reactive where Base: UINavigationBar {
var barTintColor: UIBindingObserver<Base, UIColor> {
return UIBindingObserver(UIElement: self.base) { navigationBar, barTintColor in
navigationBar.barTintColor = barTintColor
}
}
}
class BaseViewController: UIViewController {
private(set) var tintColor: Variable<UIColor> = Variable(ColorConstants.tintColor)
override func viewDidLoad() {
super.viewDidLoad()
if let navigationBar = self.navigationController?.navigationBar {
self.tintColor.asObservable()
.bind(to: navigationBar.rx.barTintColor)
.addDisposableTo(self.disposeBag)
}
}
}
class TasksListViewController: BaseViewController {
var viewModel: TasksListViewModel!
...
override func viewDidLoad() {
super.viewDidLoad()
self.viewModel.colorObsrvable
.unwrap()
.bind(to: self.tintColor)
.addDisposableTo(self.disposeBag)
self.viewModel.titleObsrvable
.unwrap()
.bind(to: self.rx.title)
.addDisposableTo(self.disposeBag)
}
}
class TasksListViewModel {
private(set) var titleObsrvable: Observable<String?>!
private(set) var colorObsrvable: Observable<UIColor?>!
init(from goal: Goal) {
let goalPredicate = NSPredicate(format: "(SELF = %@)", self.goal.objectID)
self.goalObservable = CoreDataModel.shared.rx.fetchObject(predicate: goalPredicate)
self.titleObsrvable = goalObservable.map { [=10=]?.name }
self.colorObsrvable = goalObservable.map { [=10=]?.color }
}
}
extension Reactive where Base: CoreDataModel {
func fetchObjects<T: NSManagedObject>(predicate: NSPredicate? = nil) -> Observable<[T]> {
let entityName = String(describing: T.self)
let fetchRequest = NSFetchRequest<T>(entityName: entityName)
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]
fetchRequest.predicate = predicate
return base.managedObjectContext.rx.entities(fetchRequest: fetchRequest)
}
}
class Goal
只是 class 实体
我试过的
- 是的,它是 MainThread,我测试过。还有 运行 GCD MainThread 上的这个任务 ❌
- 添加
observeOn(MainScheduler.sharedInstance)
没有帮助❌
- 在UIBindingObserver里面打断点,拖放不调用,断点不调用❌
- 将故事板中的标题添加到 NavBar 并且它可以工作,但有线延迟。首先它显示 "Ti..." 然后 "Title" ✅
已替换(及其作品✅ idk 为什么):
self.titleObsrvable = goalObservable.map { [=11=]?.name }
self.colorObsrvable = goalObservable.map { [=11=]?.color }
与:
self.titleObsrvable = Observable.of("1234")
self.colorObsrvable = Observable.of(UIColor.red)
知道我做错了什么吗?
如果你想检查整个项目,你可以在这里看到:
https://github.com/Yerkenabildin/my-everest
这似乎是某种竞争条件。您在导航控制器开始其动画的同时设置 barTintColor
。如果您确保在动画完成 之前不更改锡颜色,它将按照您想要的方式工作。
此外,您有两个不同的活页夹 rx.barTintColor 同一个导航栏,它们提供的栏有冲突的信息。如果可能的话,你应该只有一个活页夹。
我为 barTintColor
新的 UIColor
和 title
绑定到 UIBindingObserver
,但它没有显示。有线的事情是,当我将 UIViewController 拖回并释放时,一切都会出现
代码
extension Reactive where Base: UINavigationBar {
var barTintColor: UIBindingObserver<Base, UIColor> {
return UIBindingObserver(UIElement: self.base) { navigationBar, barTintColor in
navigationBar.barTintColor = barTintColor
}
}
}
class BaseViewController: UIViewController {
private(set) var tintColor: Variable<UIColor> = Variable(ColorConstants.tintColor)
override func viewDidLoad() {
super.viewDidLoad()
if let navigationBar = self.navigationController?.navigationBar {
self.tintColor.asObservable()
.bind(to: navigationBar.rx.barTintColor)
.addDisposableTo(self.disposeBag)
}
}
}
class TasksListViewController: BaseViewController {
var viewModel: TasksListViewModel!
...
override func viewDidLoad() {
super.viewDidLoad()
self.viewModel.colorObsrvable
.unwrap()
.bind(to: self.tintColor)
.addDisposableTo(self.disposeBag)
self.viewModel.titleObsrvable
.unwrap()
.bind(to: self.rx.title)
.addDisposableTo(self.disposeBag)
}
}
class TasksListViewModel {
private(set) var titleObsrvable: Observable<String?>!
private(set) var colorObsrvable: Observable<UIColor?>!
init(from goal: Goal) {
let goalPredicate = NSPredicate(format: "(SELF = %@)", self.goal.objectID)
self.goalObservable = CoreDataModel.shared.rx.fetchObject(predicate: goalPredicate)
self.titleObsrvable = goalObservable.map { [=10=]?.name }
self.colorObsrvable = goalObservable.map { [=10=]?.color }
}
}
extension Reactive where Base: CoreDataModel {
func fetchObjects<T: NSManagedObject>(predicate: NSPredicate? = nil) -> Observable<[T]> {
let entityName = String(describing: T.self)
let fetchRequest = NSFetchRequest<T>(entityName: entityName)
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]
fetchRequest.predicate = predicate
return base.managedObjectContext.rx.entities(fetchRequest: fetchRequest)
}
}
class Goal
只是 class 实体
我试过的
- 是的,它是 MainThread,我测试过。还有 运行 GCD MainThread 上的这个任务 ❌
- 添加
observeOn(MainScheduler.sharedInstance)
没有帮助❌ - 在UIBindingObserver里面打断点,拖放不调用,断点不调用❌
- 将故事板中的标题添加到 NavBar 并且它可以工作,但有线延迟。首先它显示 "Ti..." 然后 "Title" ✅
已替换(及其作品✅ idk 为什么):
self.titleObsrvable = goalObservable.map { [=11=]?.name }
self.colorObsrvable = goalObservable.map { [=11=]?.color }
与:
self.titleObsrvable = Observable.of("1234")
self.colorObsrvable = Observable.of(UIColor.red)
知道我做错了什么吗?
如果你想检查整个项目,你可以在这里看到: https://github.com/Yerkenabildin/my-everest
这似乎是某种竞争条件。您在导航控制器开始其动画的同时设置 barTintColor
。如果您确保在动画完成 之前不更改锡颜色,它将按照您想要的方式工作。
此外,您有两个不同的活页夹 rx.barTintColor 同一个导航栏,它们提供的栏有冲突的信息。如果可能的话,你应该只有一个活页夹。