ios - How to change the frame of a view when a variable value changes? -
in viewcontroller, there few views. of view's frames depends on variable
cgfloat borderwidth
these views 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 change sec1's frame when change value of borderwidth class. how can that? know
[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 frame there lots of uiviews. cant set frame of them in method.
you can implement 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, owns mssectionview subviews:
@implementation tsviewcontroller @synthesize borderwidth; - (void) viewdidload { [super viewdidload]; // additional setup after loading view, typically nib. nsarray* views = [self.view subviews]; (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;
Comments
Post a Comment