Grab the width and height of a device - iphone

I am using this code:
CGRect myRect = CGRectMake(self.frame.size.width, self.frame.size.height);
But it gives this error:
- Property "frame" not found on object of type [myViewController]
Anyone have any ideas on how I should modify my code?

First of all, you have too few arguments for the CGRectMake function. You need to define your origin. Also, you need to have self.view instead of just self. Looks something like this:
CGRect myRect = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);

the view frame of the view from within the code is being executed
CGRect myViewFrame = self.view.frame;
or the frame of the screen if that's what you want (says device in the title of the question)
CGRect screenFrame = [[UIScreen mainScreen] bounds];
or this which will return the same minus status bar if visible
CGRect actualyScreenFrame = [[UIScreen mainScreen] applicationFrame];

Use
CGRect myRect = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.heigh);
And if you want to know the device screen size, then you should use:
[UIScreen mainScreen].bounds

Related

Make UIView move down if iPhone is iPhone 5

I am currently creating an app which contains a pickerView and a tapbar within a view. The view is placed on the bottom of the screen. The tapbar is always visible. If I tap a button on the tapbar, the view will move up and show the pickerView.
My Problem is:
The view does not move down to the bottom of the iPhone 5 screen. I placed this code in different locations:
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
if (screenSize.height > 480.0f){
[UIView beginAnimations:Nil context:Nil];//tells the compiler to start a new animation
[UIView setAnimationDuration:0.6];//in seconds
self.hiddenView.frame = CGRectMake(0, 504, 320, 260);//move the picker to a specific position (504 = 568 - statusbar(20) - tapbar(44))
[UIView commitAnimations];//tells the compiler to start the animations
}
such as in the:
-(void)viewDidLoad{}
-(void)viedWillAppear{}
-(void)viewDidAppear{}
I am clueless. Does anyone know how to make it the right way?
UPADTE
Ok thanks for all the answers, but my animation works fine. My problem is that it is executed every time (checked using breakpoints) won't fire (the view won't move down). So I asked WHERE I have to put my code in order to work properly. Not if the code was correct, because it is. I know there are different ways to check if it is an iPhone 5 and to animate things, but again: this code is working and not the actual problem. I hope you understand my problem better now.
Use this below code.
if([[UIScreen mainScreen] bounds].size.height == 568)
{
//This is for iphone 5 view
//add your code here.
UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 150, 100, 100)];
}
else
{
//This is for iphone 4 view
//add your code here.
UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
}
This condition for iphone 5 .
There is no need to comparision of height for different ios:
Try this for hide your view:
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
[UIView beginAnimations:Nil context:Nil];//tells the compiler to start a new animation
[UIView setAnimationDuration:0.6];//in seconds
self.hiddenView.frame = CGRectMake(0, screenSize.height, 320, 260);//move the picker to a specific position
[UIView commitAnimations];//tells the compiler to start the animations
To show your view:
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
[UIView beginAnimations:Nil context:Nil];//tells the compiler to start a new animation
[UIView setAnimationDuration:0.6];//in seconds
self.hiddenView.frame = CGRectMake(0, screenSize.height-260, 320, 260);//move the picker to a specific position
[UIView commitAnimations];//tells the compiler to start the animations
Update
you can take a bool variable or can use selected state of your tapButton
Like:
-(IBAction) tapbuttonClicked:(UIButton *)sender
{
[sender setSelected:!sender.isSelected];
if(sender.isSelected)
{
//code to show your view...
}
else
{
//code to hide....
}
}
Ok since apparently nobody correctly understood my question (or didn't actually read it to the end?), I had to find an answer for myself. If somebody encounters the same problem here is how I solved it.
Just place your code into the -(void)viewDidLayoutSubviews{} function. It will execute as soon as the view is fully loaded.
-(void)viewDidLayoutSubviews{
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
if (screenSize.height > 480.0f){
[UIView beginAnimations:Nil context:Nil];//tells the compiler to start a new animation
[UIView setAnimationDuration:0.0];//in seconds
self.hiddenView.frame = CGRectMake(0, 504, 320, 260);//move the picker to a specific position
[UIView commitAnimations];//tells the compiler to start the animations
}
}
My favorite way of doing such things is using + (UIView) animateWithDuration: animations:
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
if (screenSize.height > 480.0f){
[UIView animateWithDuration:0.60f animations:^(void){
self.hiddenView.frame = CGRectMake(0, 504, 320, 260);//move the picker to a specific position (504 = 568 - statusbar(20) - tapbar(44))
}];
}
This should do the trick for you. Also, you can look at animateWithDuration: animations: completion: or even animateWithDuration: delay: options: animations: completion:
Hope this helps.
Add The following line of code:
#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )
At yourProjectName-perfix.pch file
This is useful for recognize that device is iPhone4 or iPhone5. by give condition such like
if (IS_IPHONE_5)
{
//// write code/setFrame for iPhone5;
}
eles
{
//// write code/setFrame for iPhone4;
}
Use it anyWhere in your project:

Why doesn't the UIView frame not set?

Am trying something so trivial! Can't believe it's not working! Maybe am overlooking something!
Am having a home page in my iPhone app. I want to now add a subview which will be initially out of the screen (i.e. hidden to the right i.e. its frame.origin.x will be the screen width), and then I want to animate its entry into the page with a flip-horizontal and show the subview partially (only half-screen approximately).
[self.mySubview.view setFrame:CGRectMake([[UIScreen mainScreen] bounds].size.width, 0.0,
[[UIScreen mainScreen] bounds].size.width,
[[UIScreen mainScreen] bounds].size.height)];
[self.view addSubview:self.mySubview.view];
NSLog(#"frame before x %f", self.mySubview.view.frame.origin.x);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[self.mySubview.view setFrame:CGRectMake([[UIScreen mainScreen] bounds].size.width*(2/5), 0.0,
[[UIScreen mainScreen] bounds].size.width,
[[UIScreen mainScreen] bounds].size.height)];
[UIView commitAnimations];
NSLog(#"frame x %f", self.mySubview.view.frame.origin.x);
Am trying to run this on an iPad. But the subview's frame is not getting set to 768.0 initially. Also, the animation is not happening. Before and after the animation, the subview's frame.origin.x is only 0.0. Where could I have gone wrong? Please help. I tried varying the autosizing arrows in the storyboard (removing them, adding them) ... nothing seems to work!
Try This
Someviewcontroller *c = initWithNibName
c.view.frame = CGRectMake(0, 0, 200, 200);
[self addSubView:c];
Try this:
self.mySubview
instend of
self.mySubview.view

What is best and simple way to get view height except navigation bar?

I want to get height of view except navigation bar. I used below method so far. Is there better way? And isn't it dangerous to hardcode height of navigation bar as 44.0?
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
viewHeight = screenRect.size.height - 44.0;
What do you want combine for ? It's simple and the best way.
If you do that often in your code you can make some macro like
#define kViewHeight CGRect screenRect = [[UIScreen mainScreen] applicationFrame]; viewHeight = screenRect.size.height - 44.0;
or something like that.

UIScrollView contentSize Issue

Within my view I create a scrollview with a width of 320 and a height of 70.
Responding to the user touching a button, I expand the scrollview, so it is 380(h) x 320(w) in size.
The code for this is shown below:
CGRect scrollviewFrame = CGRectMake(0, 30, 320, 380);
[scrollView setFrame:scrollviewFrame];
[self layoutScrollImages:YES];
CGSize srect = CGSizeMake([scrollView bounds].size.width, (kNumImages * kScrollObjHeight));
[scrollView setContentSize:srect];
As I debug this project, I can see that srect is 320 (w) x 8000 (h) in size; however my issue is the scrollable area (where the user can actually touch to scroll the scrollview) remains the same as when it was it's original size.
I'm obviously missing something, does anyone know what it is?
Edit: I have created a sample project to illustrate the issue I am having, it is available here: http://dl.dropbox.com/u/9930498/ScrollViewTest.zip
I think , you have issue with your below line of code.
CGSize srect = CGSizeMake([scrollView bounds].size.width, (kNumImages * kScrollObjHeight));
[scrollView setContentSize:srect];
debug your program and see the content of srect , Also let us know the value of below two constant
kNumImages,kScrollObjHeight .
You need to change the contentSize of the scrollview as well as the frame.
Maybe this will help aswell: UIScrollView Class Reference
Do this when setting the new size
scrollView.scrollEnabled = YES;
scrollView.showsHorizontalScrollIndicator = YES;
scrollView.showsVerticalScrollIndicator = YES;
[scrollView setFrame:CGRectMake(0, 30, 320, 380)];
CGSize srect = CGSizeMake([scrollView bounds].size.width, (kNumImages * kScrollObjHeight));
[scrollView setContentSize:srect];
[self layoutScrollImages:YES];

Change width of UIImageView

How would you change just the width of a UIImageView in objective-c? Currently I'm doing this:
imageview.frame = GCRectMake(x,y,width,height);
But when I change the width it seems to just change the position rather then change the size.
try this:
CGRect frame = [imageView frame];
frame.size.width = newWidth;
[imageView setFrame:frame];