2013-05-26 14 views
6

Tôi đang cố gắng thêm một biểu tượng nhỏ ở giữa màn hình ứng dụng iPhone của mình. Dưới đây là đoạn code tôi nghĩ nên làm việc, nhưng nó không tập trung vào nó. Các vị trí liên quan đến chiều rộng là tốt, nhưng chiều cao là cách tắt, khoảng 100 điểm ảnh tắt?Thêm trung tâm phụ của màn hình

UIImage *pinMarker = [UIImage imageNamed:@"red_pen_marker.png"]; 
UIImageView *pinMarkerView = [[UIImageView alloc] initWithImage:pinMarker]; 
pinMarkerView.frame = CGRectMake(self.view.frame.size.width/2 - 9, self.view.frame.size.height/2 - 36, 18, 36); 


[self.view addSubview:pinMarkerView]; 

Mọi đề xuất? Có thể đặt nó theo toàn bộ kích thước cửa sổ ứng dụng thay vì chế độ xem màn hình này?

+2

tôi đoán là bạn đang sử dụng một UINavigationController (có thể với một thanh công cụ và/hoặc hiển thị dấu nhắc) và rằng bạn đang thêm nó vào xem một cái nhìn khiển của, như trái ngược với các sự giao diện điều khiển điều hướng. –

Trả lời

13

Nó phải là self.view.frame.size.height/2 - 18. Một cách đơn giản hơn là chỉ cần thiết lập như sau:

pinMarkerView.center = self.view.center; 

Nhưng không biết làm thế nào để xem bố mẹ là không thể nói cách đưa imageView vào giữa màn hình.

+0

Cũng không thể. Tôi không nhớ tất cả các mã của trái tim nhưng bạn có thể nhận được trung tâm của cửa sổ và sau đó tính toán các tọa độ bằng cách yêu cầu một trong những quan điểm để tính toán nó cho bạn. –

4

Tôi nghĩ NSLayoutConstraint nên giải quyết vấn đề của bạn

UIView *superview = self.view; 

UIImage *pinMarker = [UIImage imageNamed:@"red_pen_marker.png"]; 
UIImageView *pinMarkerView = [[UIImageView alloc] initWithImage:pinMarker]; 
pinMarkerView.frame = CGRectMake(self.view.frame.size.width/2 - 9, self.view.frame.size.height/2 - 36, 18, 36); 

[pinMarkerView setTranslatesAutoresizingMaskIntoConstraints:NO]; 
[self.view addSubview:pinMarkerView]; 

NSLayoutConstraint *myConstraint =[NSLayoutConstraint 
            constraintWithItem:pinMarkerView 
            attribute:NSLayoutAttributeCenterX 
            relatedBy:NSLayoutRelationEqual 
            toItem:superview 
            attribute:NSLayoutAttributeCenterX 
            multiplier:1.0 
            constant:0]; 
NSLayoutConstraint *myConstraint2 =[NSLayoutConstraint 
            constraintWithItem:pinMarkerView 
            attribute:NSLayoutAttributeCenterY 
            relatedBy:NSLayoutRelationEqual 
            toItem:superview 
            attribute:NSLayoutAttributeCenterY 
            multiplier:1.0 
            constant:0]; 

[superview addConstraint:myConstraint]; 
[superview addConstraint:myConstraint2]; 
3

Dưới đây là một giải pháp

Thay vì self.view.frame.size.width

Sử dụng :: [ UIScreen mainScreen] .bounds.size.width hoặc height

Vì vậy, mã là

UIImage *pinMarker = [UIImage imageNamed:@"red_pen_marker.png"]; 
UIImageView *pinMarkerView = [[UIImageView alloc] initWithImage:pinMarker]; 
pinMarkerView.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.height/2 , 18, 36); 
[self.view addSubview:pinMarkerView];