简体   繁体   中英

How to change the frame of a view when a variable value changes?

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.

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;

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) )];
}

You can consider to use KVO. Observe borderWidth 's change in your sec1 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.
        }
    }
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM