1

I'm using RxSwift to observe the frame of one UIView. Here is my code in the view

self.rx.observe(CGRect.self, #keyPath(UIView.frame))
    .subscribe(onNext: { frame in
        print("Got new frame \(frame)")
    })
    .debug()
    .disposed(by: disposeBag)

But it only prints once and won't run the second time when I change view frame. Does anyone know why this happen? Thanks in advance!

duan
  • 8,515
  • 3
  • 48
  • 70
RayChen
  • 1,417
  • 2
  • 17
  • 36
  • 4
    Actually you can't, check this [UIView frame/position binding](https://stackoverflow.com/questions/44024755/bind-two-uiview-fram-position-using-rxswift) – mojtaba al moussawi Aug 29 '18 at 18:26
  • There are undocumented ways of observing this apparently... if you want to live dangerously: [KVO for a UIView frame](https://stackoverflow.com/questions/4874288/how-can-i-do-key-value-observing-and-get-a-kvo-callback-on-a-uiviews-frame/19687115#19687115) – Daniel T. Aug 31 '18 at 02:14

1 Answers1

0

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.

Hwangho Kim
  • 629
  • 4
  • 20