2013-05-20 125 views
0

在我的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)) ]; 

我想改变SEC1的帧时,我的borderwidth值从另一个类而改变。 我们该怎么做? 我知道

[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的。所以我不能在这个方法中为它们设置框架。

+0

所有的子视图的框架计算相同的方式? –

回答

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

我的所有部分都没有相同的框架。每个帧都不相同。 – manujmv

+0

一旦检查我编辑的答案 – Balu

+0

我知道它会起作用。如果只有一个uiview,很容易。但我有大约40 uiview与不同的框架。所以我不能在这个方法中设置它们中的每一个 – manujmv

0

您可以考虑使用KVO。观察borderWidth在您的sec1课程中的变化。这样,如果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. 
     } 
    } 
} 
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 

在视图控制器,它拥有一些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;