2013-05-20 2 views
0

내 viewcontroller에서 몇 가지보기가 있습니다. 해당 뷰의 모든 프레임은 변수변수 값이 변경 될 때보기의 프레임을 변경하는 방법은 무엇입니까?

CGFloat borderWidth

이러한 전망에 따라이

내가 다른 클래스에서 borderwidth의 값을 변경할 때 SEC1의 프레임을 변경하려면
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 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(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가 있다면 그것은 쉽습니다. 하지만 난 다른 프레임 uiview 약 40있다. 그래서 나는이 방법으로 각각을 설정할 수 없다. – manujmv

0

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

당신은 다음과 같이 구현할 수 있습니다 MSSectionView.m에서

:

@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에서 : 파단과 같은 일부 MSSectionView 소유의 ViewController에서

#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 

, :

@interface TSViewController : UIViewController 
{ 
    CGFloat borderWidth; 
} 

@property(nonatomic,readwrite,assign)CGFloat borderWidth; 
관련 문제