0

In my viewcontroller, there are few views. All of that view's frames are depends on the variable

CGFloat borderWidth

These views are defined like

sec1 = [[MSSectionView alloc]initWithFrame:CGRectMake(self.borderWidth, self.borderWidth,self.frame.size.width/2-(self.borderWidth*3/2),self.frame.size.height/2 - (self.borderWidth*3/2) ) ];

i want to change the sec1's frame when i change the value of borderwidth from another class. how can we do that? I know that

[sec1 setFrame:CGRectMake(self.borderWidth, self.borderWidth,self.frame.size.width/2-(self.borderWidth*3/2),self.frame.size.height/2 - (self.borderWidth*3/2) )];

will change the frame but there are lots of uiviews. so i cant set frame for all of them in this method.

manujmv
  • 6,450
  • 1
  • 22
  • 35

3 Answers3

1

You can implement like this:

In MSSectionView.m:

#import "MSSectionView.h"

@implementation MSSectionView

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString: @"borderWidth"])
    {
        CGFloat borderWidth = [(NSNumber*)[change objectForKey:NSKeyValueChangeNewKey] floatValue];
        [self setFrame: CGRectMake(borderWidth, borderWidth, self.frame.size.width/2 - (borderWidth * 3/2), self.frame.size.height/2 - (borderWidth * 3/2))];
    }
    else
    {
        [super observeValueForKeyPath: keyPath
                             ofObject: object
                               change: change
                              context: context];
    }
}

@end

In viewcontroller, which owns some MSSectionView as subviews:

@implementation TSViewController

@synthesize borderWidth;

- (void) viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    NSArray* views = [self.view subviews];

    for (UIView* subview in views)
    {
        if ([subview isKindOfClass: [MSSectionView class]])
        {
            MSSectionView* sectionView = (MSSectionView*) subview;
            [self addObserver: sectionView
                   forKeyPath: @"borderWidth"
                      options: NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
                      context: NULL];
        }

    }
}

In viewcontroller.h:

@interface TSViewController : UIViewController
{
    CGFloat borderWidth;
}

@property(nonatomic,readwrite,assign)CGFloat borderWidth;
stosha
  • 2,108
  • 2
  • 27
  • 29
0

For achieving this one take one for loop and change all the frames.

for(UIView *subview in yourview.subviews)
{
    // Here you can change your frames based on the condition
    //here you will get old frame value of subview so you can change by using the old frame values of all the subviews.
    [subview setFrame:CGRectMake(self.borderWidth, self.borderWidth,self.frame.size.width/2-(self.borderWidth*3/2),self.frame.size.height/2 - (self.borderWidth*3/2) )];
}
Balu
  • 8,470
  • 2
  • 24
  • 41
  • all of my section haven't same frame. frames are different for each. – manujmv May 20 '13 at 06:06
  • i know it will works. it is easy if there is only one uiview. but i have around 40 uiview with different frame . so i can't set each of them in this method – manujmv May 20 '13 at 06:11
0

You can consider to use KVO. Observe borderWidth's change in yoursec1 class. In this way, if borderWidthe change, your sec1 class can change accordingly.

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self addObserver:self forKeyPath:@"borderWidth" options:NSKeyValueObservingOptionNew context:&self->borderWidth];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if (context == &self.borderWidth) {
        if ([keyPath isEqualToString:@"borderWidth"]) {
            // Calculate your subview's frame.
        }
    }
}
sunkehappy
  • 8,970
  • 5
  • 44
  • 65