Bind two UIview fram/Position using Rxswift
Asked Answered
R

3

6

I want to change view2 position automatically when view1 position will change and bind the both view position using Rxswift.

I try to observe view frame/position using this

view.rx.observe(CGRect.self,"frame")
        .subscribe(onNext: {
            print($0 ?? (0,0))
        })

it print frame on init time but when change view position using constraints

self.constraintHorizontalCenterView.constant = 1000

it print nothing means this code not observe view position...

Is there any way to observe continuously view position or bind view position?

Raymundorayna answered 17/5, 2017 at 12:22 Comment(0)
D
2

Whether the frame reflect the actual location or not, you could observe with frame like this.

This is old post, but I share my code. It works correctly.

   self.mapView
            .rx
            .observe(CGRect.self, #keyPath(UIView.frame))
            .asDriver(onErrorJustReturn: CGRect.zero)
            .drive(onNext: { (rect) in
                log.debug("Frame changed to \(String(describing: rect))")
            }).disposed(by: self.disposeBag)

In here, I tried to observe the mapView's frame with using Driver.

Hope this help to you.

Duad answered 4/3, 2020 at 1:7 Comment(0)
G
1

from apple 'frame' // animatable. do not use frame if view is transformed since it will not correctly reflect the actual location of the view. use bounds + center instead.

You can observe bound or center to instead.

Gallon answered 23/5, 2017 at 8:39 Comment(0)
M
-1

UIView.frame is not KVO compliant so you can't directly observe it. In any case, doing so is inappropriate IMHO.

Some event is causing your view to change size. Observe that instead.

Maryettamaryjane answered 17/5, 2017 at 13:59 Comment(2)
Actually frame is KVO.Lunitidal
"Nothing in UIKit is guaranteed to be KVO-compliant. If you happen to find that KVO-ing a property works, be grateful, it's unintentional. Also: be wary. It could very well break in the future." -- Dave DeLong. The quote comes from when he was an Evangelist working for Apple. He worked for Apple from 2010-2017.Maryettamaryjane

© 2022 - 2024 — McMap. All rights reserved.