Adding subview center of screen
Asked Answered
B

3

6

I am trying to add a small icon in the center of my iPhone app screen. Below is the code I think should work, but it isn't centering it. The position regarding the width is fine, but the height is way off, about 100 pixels off?

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

Any suggestions? Maybe placing it according to the entire app window size instead of this screens view?

Boult answered 26/5, 2013 at 23:17 Comment(1)
My guess is that you are using a UINavigationController (possibly with a toolbar and/or prompt showing) and that you're adding it to a view controller's view, as opposed the the navigation controller's view.Barmen
P
14

It should be self.view.frame.size.height/2 - 18. A simpler way is to just set it like this:

pinMarkerView.center = self.view.center;

But without knowing what the parent view is it is impossible to tell how to bring the imageView to the center of the screen.

Packthread answered 26/5, 2013 at 23:35 Comment(1)
Well it's not impossible. I don't remember all the code by heart but you can get the center of the window and then calculate the coordinate by asking one of the views to calculate it for you.Early
H
4

I think NSLayoutConstraint should solve your problem

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];
Hellkite answered 27/5, 2013 at 7:12 Comment(0)
S
3

Here is a solution

Instead of self.view.frame.size.width

Use:: [UIScreen mainScreen].bounds.size.width or height

So code is

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];
Salvatore answered 12/11, 2014 at 6:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.