6

How come the UILabel drawn in this code is not in the center of the view?

//create the view and make it gray
UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor darkGrayColor];

//everything for label
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,42,21)];

//set text of label
NSString *welcomeMessage = [@"Welcome, " stringByAppendingString:@"username"];
welcomeMessage = [welcomeMessage stringByAppendingString:@"!"];
label.text = welcomeMessage;

//set color
label.backgroundColor = [UIColor darkGrayColor];
label.textColor = [UIColor whiteColor];

//properties
label.textAlignment = NSTextAlignmentCenter;
[label sizeToFit];

//add the components to the view
[view addSubview: label];
label.center = view.center;

//show the view
self.view = view;

The line, label.center = view.center; should move the label to the center of the view. But instead moves it to where the center of the label is in the left hand corner of the view as shown below.

screenshot
(source: gyazo.com)

Does anyone know why?

Community
  • 1
  • 1
Dummy Code
  • 1,858
  • 4
  • 19
  • 38
  • 1
    First of all why you do `self.view=view` ? You don't need that, second your `view` doesn't have a frame set. Third, use label.center = self.view.center. – danypata Jun 05 '13 at 21:01
  • First, because I am building a view within an application. Second, How do I do that. And third, I use `view.center` because I am creating a new view. I need to set the view to `UIView *view` using `self.view= view` – Dummy Code Jun 05 '13 at 21:02
  • I suppose that this code is in `-viewDidLoad`. Right? – Mohannad A. Hassan Jun 05 '13 at 21:04
  • For second, alloc/init for the view, view.frame = CGRectMake(...) – danypata Jun 05 '13 at 21:04
  • @MohannadA.Hassan Yes, my apologies, should of clarified. – Dummy Code Jun 05 '13 at 21:04
  • @danypata Paramaters being, (0,0,320,280)? – Dummy Code Jun 05 '13 at 21:06
  • Depends, if you want full screen then should be (0,0,320,480) if not it can be any value. – danypata Jun 05 '13 at 21:08
  • I agree with @danypata. You don't need to create a new view. In `viewDidLoad`, it's already created. You can change its `backgroundColor` directly. – Mohannad A. Hassan Jun 05 '13 at 21:08
  • @MohannadA.Hassan This is just a test app for another part of an app I am making, I am just testing components with this view, then I can copy and paste into my other app when I am done. – Dummy Code Jun 05 '13 at 21:09
  • @danypata Ah... I thought I remembered 320 x n80 or something like that. Haha. – Dummy Code Jun 05 '13 at 21:10
  • You get a view in viewDidLoad: method itself. Just change its various properties for that only. Why create a new view and replacing the old one just for changed properties. – Zen Jun 05 '13 at 21:11
  • 1
    Yes and thats why I explained too in your last question too. Without a provided frame, a view has CGRectZero frame i.e. (0,0,0,0). So always set a frame for UIViews. – Zen Jun 05 '13 at 21:14
  • There is no need to create another view in `viewDidLoad` just to ad a button. See GuillaumeA's answer. – ott-- Jun 05 '13 at 21:20
  • @ott and see my comment for the explanation. Thanks. – Dummy Code Jun 05 '13 at 21:21

3 Answers3

5

You need to init your view with a frame:

UIView *view = [[UIView alloc] initWithFrame:self.view.frame];
Corey
  • 5,818
  • 2
  • 24
  • 37
4

This is caused by your view variable not having a frame defined. By default it has a frame set to (0, 0, 0, 0), so its center is (0, 0).

Hence when you do label.center = view.center;, you set the center of your label to (0 - label.width / 2, 0 - label.height /2). (-80.5 -10.5; 161 21) in your case.

There is no need for a new UIView if your UIViewController already have one, just work with self.view.

- (void)viewDidLoad
{
    [super viewDidLoad];

    //create the view and make it gray
    self.view.backgroundColor = [UIColor darkGrayColor];

    //everything for label
    UILabel *label = [[UILabel alloc] init];

    //set text of label
    // stringWithFormat is useful in this case ;)
    NSString *welcomeMessage = [NSString stringWithFormat:@"Welcome, %@!", @"username"];
    label.text = welcomeMessage;

    //set color
    label.backgroundColor = [UIColor darkGrayColor];
    label.textColor = [UIColor whiteColor];

    //properties
    label.textAlignment = NSTextAlignmentCenter;
    [label sizeToFit];

    //add the components to the view
    [self.view addSubview: label];
    label.center = self.view.center;
}

Also note that doing label.center = self.view.center will not work properly when rotating to landscape mode.

Community
  • 1
  • 1
Guillaume Algis
  • 10,705
  • 6
  • 44
  • 72
0

Your code would work fine if you put it in the viewDiLayoutSubviews instead of viewDidLoad