0

在我的视图控制器中,视图很少。该视图的所有框架都取决于变量

CGF浮动边框宽度

这些视图的定义如下

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

会改变框架,但有很多uiviews。所以我不能用这种方法为所有这些设置框架。

4

3 回答 3

1

你可以这样实现:

在 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;
于 2013-05-20T10:19:47.613 回答
0

为了实现这一点,需要一个 for 循环并更改所有帧。

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) )];
}
于 2013-05-20T05:40:29.710 回答
0

你可以考虑使用KVO。观察borderWidthsec1班级的变化。这样,如果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.
        }
    }
}
于 2013-05-20T06:40:29.513 回答