iphone - Resize a UIView -
i have 2 uiview
s, containerview
, testview
. width of testview
bigger of containerview
. in such case, when add testview
subview containerview
, there comes problem. frames of testview
goes beyond bounds of containerview
. here code -
- (void)viewdidload { [super viewdidload]; // additional setup after loading view, typically nib. uiview *containerview = [[uiview alloc] initwithframe:cgrectmake(60, 154, 200, 200)]; containerview.backgroundcolor = [uicolor graycolor]; containerview.clipstobounds = no; uiview *testview = [[uiview alloc] initwithframe:cgrectmake(0, 0, 260, 100)]; testview.backgroundcolor = [uicolor yellowcolor]; testview.layer.borderwidth = 3; testview.layer.bordercolor = [[uicolor blackcolor] cgcolor]; testview.center = cgpointmake(containerview.frame.size.width / 2, containerview.frame.size.height / 2); uilabel *label = [[uilabel alloc] initwithframe:cgrectmake(15, 25, 230, 50)]; label.text = @"this label test size"; [testview addsubview:label]; [containerview addsubview:testview]; [self.view addsubview:containerview]; }
output code -
when containerview.clipstobounds = yes;
, output -
but need - (edited image)
when add testview
width/height bigger superview- containerview
, frame of testview
(including children) should adjusted that, fits inside superview.
i welcome ideas..!
the modern way create view, add constraints view establish relationship containing view , add view subview.
that way aren't messing frames, , resizing rotation handled you.
edited add
here example code similar asking for
- (void)viewdidload { [super viewdidload]; // create containing view uiview *greyview = [[uiview alloc] initwithframe:cgrectzero]; greyview.backgroundcolor = [uicolor graycolor]; greyview.translatesautoresizingmaskintoconstraints = no; [self.view addsubview:greyview]; // create label go containing view uilabel *label = [[uilabel alloc] initwithframe:cgrectzero]; label.translatesautoresizingmaskintoconstraints = no; label.text = @"example text"; label.backgroundcolor = [uicolor yellowcolor]; [greyview addsubview:label]; nsdictionary *bindings = nsdictionaryofvariablebindings(greyview, label); // set constraints greyview relative it's superview - cover [self.view addconstraints:[nslayoutconstraint constraintswithvisualformat:@"h:|[greyview]|" options:nslayoutformatdirectionleadingtotrailing metrics:nil views:bindings]]; [self.view addconstraints:[nslayoutconstraint constraintswithvisualformat:@"v:|[greyview]|" options:nslayoutformatdirectionleadingtotrailing metrics:nil views:bindings]]; // set constraints label pinned equally padding [greyview addconstraints:[nslayoutconstraint constraintswithvisualformat:@"h:|-(30)-[label]-(30)-|" options:nslayoutformatdirectionleadingtotrailing metrics:nil views:bindings]]; [greyview addconstraints:[nslayoutconstraint constraintswithvisualformat:@"v:|-(30)-[label]-(30)-|" options:nslayoutformatdirectionleadingtotrailing metrics:nil views:bindings]]; }
if want see in action , tweak constraints yourself, i've uploaded simple example project can use.
Comments
Post a Comment