View Full Version : How to get and change the height and width in UIView

Oct 26, 2009, 11:07 AM

I am trying to learn how to creat iphone apps, hence, new to this. I am using both Interface Builder and of course Xcode.

There are a couple of things I would like to find out how to do, have searched but could find any helpful info.

1. I would like to find out the height and width of my UIView that I am using. I tried this but all it spits out is '0', which of cours can't be true.


I am trying to find out the width (and height) in the drawRect function of my UIView class that is connected to the view in Interface Builder.

It seems like the coordinates defined in Interface Builder is not the same as it turns out in the application (due to images of a known size does not fit within the view, even though it should...).

2. I would like to set the coordinates in order to make sure I know what I have to work with. How?

3. In Interface Builder, it is possible to set the status bar to "none", but when launching the application in the simulator it's still visible, how come?

4. By setting the coordinates myself, does it help out making the application look correctly scaled in other iPhone devices with different resolution?

Thank you in advance!


Oct 26, 2009, 12:04 PM
Lets start from the top. How are you getting that 0. Post your code. Normally when people say that they are using NSLog with the wrong format string. Once we sort that we can look at the other issues/questions.

Oct 26, 2009, 01:08 PM

Thank you for your answer!

Ok, you pointed me to the right direction, I wrote the width and height as an int, using an float made the value come out as supposed to. Thank you!

But now, when I define the bounds by doing this "self.bounds = CGRectMake(0, 0, 480, 320);", the view seems to change the size of itself (the view is defined to height 300 in Interface Builder), instead of just repositioning/changing the size of the content. Seems like I may have missunderstood the purpose of bounds, haven't I? The size of the view is fine, just want to change the internal reference width and height (is that the correct way of expressing it?).

- (void)drawRect:(CGRect)rect {

self.bounds = CGRectMake(0, 0, 480, 320); //Landscape

NSLog(@"Board view w %f", self.bounds.size.width);
NSLog(@"Board view h %f", self.bounds.size.height);

//Adding images here

[self setNeedsDisplay];


Oct 26, 2009, 01:39 PM
I'm a bit confused as to what you are trying to achieve. Normally you should not change the bounds in drawRect:. As described by the documentation "The bounds rectangle determines the origin and scale in the view’s coordinate system within its frame rectangle. Setting this property changes the value of the frame property accordingly."

If you want to change some sort of internal margin/padding set your own properties and take them into account when drawing your custom content.

Oct 27, 2009, 04:34 AM
Hello again,

I been trying to go through the documentation again and doing some tests in order to be able to describe my problem a bit better.

What I am trying to do is create some kind of bric-game (see attachement). The view which I am creating in Interface Builder is defined (by default) to a height of 300 in Interface Builder.

I want to be able to utilize the entire screen, without the status bar.

I assume that the status bar is stealing 20 px from me, which I want to use. How come it is still visible even though I am defining "none" for the status bar property (as well as for top and bottom bar, see attachements) in Interface Builder? How do I remove it in order for me to be able to use the entire screen? Couldn't find any info regarding this!

Anything else I have to think of in order to start using the entire screen?



Oct 27, 2009, 04:41 AM
That setting in Interface Builder is to let you see what it would like like hidden: it has no actual effect outside of interface builder. You can hide the status bar programatically (so for example you could show it in your menu screen and only hide it when the actual game was running) using the well documented setStatusBarHidden:animated: (http://developer.apple.com/IPhone/library/documentation/UIKit/Reference/UIApplication_Class/Reference/Reference.html#//apple_ref/occ/instm/UIApplication/setStatusBarHidden:animated:). Or if you want it hidden right from launch then use the similarly well documented UIStatusBarHidden (http://developer.apple.com/mac/library/documentation/CoreFoundation/Conceptual/CFBundles/BundleTypes/BundleTypes.html) key/value pair in your Info.plist file.

Oct 27, 2009, 04:43 AM
Well, and now I did find the answer myself...

In applicatinDidFinishLaunching, one should add this

[application setStatusBarHidden:YES animated:NO];

To define the status bar property is only a hint that it should be hidden. I don't understand why it isn't enough, feels like I am doing the same job twice now.

I guess it is possible to fix it in the .plist file as well, but I choose not to look into that now since I have a working solution.

So, I guess all my questions are answered now. The problem was, at the end, that I didn't know how to remove the status bar in order to utilize the entire 320 px of height.

Thank you robbieduncan for guiding me in the correct direction :)


Oct 27, 2009, 04:47 AM
Ooops, sorry for redundant information. We posted it at the same time. But the total answer is a bit more informative, thanks for now!

Oct 27, 2009, 08:54 AM
To define the status bar property is only a hint that it should be hidden. I don't understand why it isn't enough, feels like I am doing the same job twice now.
Notice how these things are called "Simulated Interface Elements"? So, they really are only intended to give you an idea of what your screen looks like if they are on. They are not meant to actually affect those parts of the UI. Hence the term "simulated" vs. "actual".