0

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.

daydr3amer
  • 326
  • 2
  • 19
  • 2
    Unfortunately I don't have time to write any example code but, rather than hard-coding specific numbers (480.0f or a frame size), Apple recommend you use the Auto-Layout / Constraints system which will also implicitly animate your changes. Of course, this is only useful if you are targeting iOS 5+. – Robotic Cat Jun 26 '13 at 11:00
  • Ok this I think would do it, but I don't get how to make the right constraints. I tried a few things via the interface builder but it confused me more then it helped. – daydr3amer Jun 26 '13 at 11:43
  • Have a look at http://stackoverflow.com/questions/17288202/different-label-size-in-different-ios-device/17292509#17292509 – Popeye Jun 26 '13 at 11:52

5 Answers5

1

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 .

Darshan Kunjadiya
  • 3,323
  • 1
  • 29
  • 31
  • Allocating a new view didn't work. It just moves back to the iPhone 4 position, instead of moving to the new CGRectMake position. – daydr3amer Jun 26 '13 at 11:31
1

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....
       }
}
Prateek Prem
  • 1,544
  • 11
  • 14
1

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
        }
    }
daydr3amer
  • 326
  • 2
  • 19
0

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:

iPatel
  • 46,010
  • 16
  • 115
  • 137
0

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.

nemesis
  • 1,349
  • 1
  • 15
  • 30
  • Thanks for the answer, but it didn't help me. I thought maybe the delay element would do it, but it didn't. The view just moved to the iPhone 4 position a second later. It doesn't matter what CGRechtMake I define, it will always move to the iPhone 4 position. – daydr3amer Jun 26 '13 at 11:29