I have a scenario like this: View B frame depends of View A frame. I am playing around with viewA frame. But the view b frame doesn't get changed because frame is not a object. So what the workaround, if I need to change the View B frame wrt to View A Frame ie view B frame gets changed when I change ViewA Frame
One Solution is add KVO
for View A
as it will notify
when frame changed
and according to change
in frame
of view A
, change
frame
of view B
[A addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionOld context:NULL]; //added KVO for View A
Method would be:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if([keyPath isEqualToString:@"frame"]) {
//change frame of View B according to View A
}
}
EDIT : Remove observer
when not needed
[A removeObserver:self forKeyPath:@"frame"];
In >iOS6, you can try to setup some autolayout constraints, or try this way:
[self addObserver:self forKeyPath:@"viewA.frame" options:NSKeyValueObservingOptionOld context:NULL];
Be sure to removeObserver on dealloc. Then, you'll find out when viewA frame changes, and you can implement any set of actions and any functional relationship between viewA and viewB frames...
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if([keyPath isEqualToString:@"viewA.frame"]) {
// change viewB's frame according to viewA's frame
viewB.frame = CGRectOffset(viewA.frame, 20.0, 20.0);
}
}
Set the View B frame with respect to View A . Write a method -(void)ResizeFrame
and put all frame settings in it and call this function when ever you need to change frame. ie ViewB.frame=[ViewA.frame.origin.x,ViewA.frame.origin.y,ViewA.frame.size.width/2,ViewA.frame.size.height/2];
Here i have given division you can change it accordingly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.