2013-06-05 31 views
5

Làm cách nào để mã số UILabel được vẽ trong mã này không nằm ở giữa của view?UILabel được đặt ở giữa chế độ xem

//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; 

Dòng, label.center = view.center; nên di chuyển label đến trung tâm của view. Nhưng thay vào đó, hãy di chuyển đến vị trí trung tâm của label nằm ở góc bên trái của view như được hiển thị bên dưới.

screenshot http://gyazo.com/2d1c064a671e32c7eb004647232fa430.png

Có ai biết tại sao không?

+1

Trước hết tại sao bạn làm 'self.view = xem'? Bạn không cần điều đó, thứ hai 'view' của bạn không có bộ khung. Thứ ba, sử dụng label.center = self.view.center. – danypata

+0

Đầu tiên, vì tôi đang xây dựng một khung nhìn trong một ứng dụng. Thứ hai, Làm thế nào để tôi làm điều đó. Và thứ ba, tôi sử dụng 'view.center' vì tôi đang tạo một khung nhìn mới. Tôi cần đặt chế độ xem thành chế độ xem 'UIView *' bằng cách sử dụng' self.view = view' –

+0

Tôi giả sử rằng mã này nằm trong '-viewDidLoad'. Đúng? –

Trả lời

4

Bạn cần init nhìn của bạn với một khung:

UIView *view = [[UIView alloc] initWithFrame:self.view.frame]; 
+0

Cảm ơn câu trả lời đơn giản, đúng. :) –

4

Điều này là do biến số view của bạn không có khung được xác định. Theo mặc định, nó có một khung được đặt thành (0, 0, 0, 0), do đó trung tâm của nó là (0, 0).

Do đó, khi bạn làm label.center = view.center;, bạn đặt trung tâm của nhãn thành (0 - label.width/2, 0 - label.height /2). (-80.5 -10.5; 161 21) trong trường hợp của bạn.

Không cần phải có UIView mới nếu UIViewController của bạn đã có, chỉ cần làm việc với 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; 
} 

Cũng lưu ý rằng thực hiện label.center = self.view.centerwill not work properly when rotating to landscape mode.

0

Mã của bạn sẽ làm việc tốt nếu bạn đặt nó trong viewDiLayoutSubviews thay vì viewDidLoad

Các vấn đề liên quan