Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RACObserve bounds or frame of a UIView?

I create a signal to observe the width of a text field's frame in -viewDidLoad:

RACSignal *destinationDisplayWidthSignal =
    [RACObserve(self.destinationNumberTextField, frame)
        map:^id(NSValue *value) {
            NSLog(@"rect: %@", NSStringFromCGRect([value CGRectValue]));
            return @(CGRectGetWidth([value CGRectValue]));
        }];

However, the signal did not send the next event after the frame changes in -viewDidAppear:. I try to replace frame with bounds and it works!

Should I always observe the bounds of a UIView using RACObserve()?

like image 751
Xaree Lee Avatar asked Feb 11 '23 08:02

Xaree Lee


1 Answers

There's no one answer, some code will call -setFrame:, while other code will call -setBounds:. If the code that sets the rect is not your code, then you could observe both bounds and frame and merge the two signals. For example:

[[[RACSignal
    merge:@[RACObserve(view, frame), RACObserve(view, bounds)]]
    logNext]
    map:^(NSValue *value) {
        return @(CGRectGetWidth([value CGRectValue]));
    }]
like image 195
Dave Lee Avatar answered Feb 27 '23 22:02

Dave Lee