Ios 当变量值更改时,如何更改视图的框架?

Ios 当变量值更改时,如何更改视图的框架?,ios,objective-c,deep-copy,shallow-copy,cgrectmake,Ios,Objective C,Deep Copy,Shallow Copy,Cgrectmake,在我的viewcontroller中,视图很少。该视图的所有帧都取决于变量 CGFloat边框宽度 这些视图的定义如下 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) ) ];

在我的viewcontroller中,视图很少。该视图的所有帧都取决于变量

CGFloat边框宽度

这些视图的定义如下

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) ) ];
当我从另一个类更改
borderwidth
的值时,我想更改sec1的帧。 我们怎么能做到呢? 我知道

[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))


将更改框架,但有许多视图。因此,我不能用这种方法为所有这些设置框架

要实现这一点,请以一为循环,并更改所有帧

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

你可以考虑使用KVO。观察
sec1
类中
borderWidth
的变化。这样,如果
borderWidthe
发生更改,您的
sec1
类可以相应地更改

- (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.
        }
    }
}

您可以这样实现:

在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
在viewcontroller中,它拥有一些MSSectionView作为子视图:

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

    }
}
在viewcontroller.h中:

@interface TSViewController : UIViewController
{
    CGFloat borderWidth;
}

@property(nonatomic,readwrite,assign)CGFloat borderWidth;

我所有的部分都没有相同的框架。每个框架都不同。我知道它会起作用。如果只有一个uiview,这很容易。但我有大约40个不同框架的uiview。所以我不能用这种方法设置它们所有子视图的帧都是用同样的方法计算的?