mardi 30 juin 2015

Dynamically change UIView frame and subview frame

I came across a problem when dealing with dynamically changing UIView's and subview's frame. Here is my below code.

I have 3 UI elements created in xib. dynamicView, dynamicLabel and a button. On button click, I am trying to resize the UIView's frame and UILabel's frame.

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *dynamicLabel;
@property (weak, nonatomic) IBOutlet UIView *dynamicView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.dynamicLabel.backgroundColor = [UIColor redColor];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)changeFrame:(id)sender {
    self.dynamicView.frame = CGRectMake(2, 2, 60, 60);
    //[self.dynamicView addSubview:self.dynamicLabel];
    self.dynamicLabel.frame = CGRectMake(0, 0, 30, 20);
}
@end

As per the code, the expected result is, the dynamicView resize to (2, 2, 60, 60) and the dynamicLabel which is a subview of dynamicView, resize to (0, 0, 30, 20) with respect to its superview, dynamicView. But in the actual result, dynamicView is resized correctly and dynamicLabel is resized with respect to self.view. Attached the screenshot. enter image description hereenter image description hereenter image description here

What am I missing to bring the expected result?

Aucun commentaire:

Enregistrer un commentaire