Make UIView move down if iPhone is iPhone 5 - iphone

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:

Related

how can I get and modify the display screen size in xcode4.3

I am developing an app, and I want it can be used on iPhone 4 and 5, as the iPhone 5 had changed its size to 4-inch, so I want my app can be supported 4-inch, but in Xcode4.3 there is no autolayout, because of this , I wonder if there is anyway to get the display screen size and change it to 4-inch, thank you very much!
You can get the screen size by using this code:
CGRect screenBounds = [[UIScreen mainScreen] bounds];
UIViewController *ViewController;
[self loadall];
if (screenBounds.size.height == 568) {
NSLog(#"User is using an iPhone 5+");
BOOL isUsingiPhone5 = YES;
}
else{
NSLog(#"User is using an iPhone 4s-");
BOOL isUsingiPhone5 = NO;
}
then you could move things accordingly, with CGRect heres an example of moving an ad banner view as a result of the screen size:
-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
CGRect bannerFrame = banner.frame;
CGRect screenBounds = [[UIScreen mainScreen] bounds];
UIViewController *ViewController;
if (screenBounds.size.height == 568) {
//iPhone5+
bannerFrame.origin.x = 0;
bannerFrame.origin.y = 518;
banner.frame = bannerFrame;
}
else{
//iPhone4s-
}
NSLog(#"Showing ad, internet connection found.");
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[banner setAlpha:1];
[UIView commitAnimations];
}
else{
[banner setAlpha:0];
}
Make sure to use ViewController as the UIViewController variable, even if your default view controller is named something like RootViewController.
Hope this helps. Feel free to comment if you have any questions!

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

iphone development: random animation

In my iphone application I have several images. lets say some small square images. what I want is I want that small objects appear from the top of the screen at rondom times and leaves the screen from the bottom. I mean, for example, object randomly appeared from the x:30 y:0 point (x value must be random) and leaves from the x:30 y:460 with an animation.
I am programming an 2d racing game and they are the cars. hope it is clear.
edit: well I tried to change the frame the numbers belong to something else. But I could not have the frame of the image and also I could not figure out how to generate position randomly more than one time. since I was planning to put it on the viewDidLoad.
[UIView beginAnimations:#"myAnimation" context:nil];
CGRect Frame = image.frame;
if(Frame.origin.y == 340){
Frame.origin.y = 260;
}else{
Frame.origin.y = 340;
}
image.frame = Frame;
[UIView commitAnimations];
Your example code doesn't show what you are asking for. It's also really hard to guess what you want.
Here's my literal interpretation of what you have asked for
for (int i = 0; i < 100; i++) {
CGRect startFrame = CGRectMake(arc4random_uniform(320), -50, 50, 50);
CGRect endFrame = CGRectMake(arc4random_uniform(320),
CGRectGetHeight(self.view.bounds) + 50,
50,
50);
UIView *animatedView = [[UIView alloc] initWithFrame:startFrame];
animatedView.backgroundColor = [UIColor redColor];
[self.view addSubview:animatedView];
[UIView animateWithDuration:2.f
delay:i * 0.5f
options:UIViewAnimationCurveLinear
animations:^{
animatedView.frame = endFrame;
} completion:^(BOOL finished) {
[animatedView removeFromSuperview];
}];
}
If you stick this in viewDidAppear: then you'll see 100 objects animate in the way you want. Obviously this is extremely inefficient and you may want to look at recycling the views etc but this shows how to animate views with UIView animation with some random points

Cannot change UIView frame size

I have a custom uiview that i am trying to update the frame size. I do it like this in a method once it is clicked. The method is called, and the frame value changes, however on the screen nothing happens!
if (!touched) {
GameBox *box = (GameBox *)[self.view viewWithTag:40];
[box setFrame:CGRectMake(20, 20, 100, 73)];
NSLog(#"%f", box.frame.origin.x);
markButton.enabled = NO;
guessButton.enabled = NO;
[self.view reloadInputViews];
NSLog(#"The action bar should now be hidden");
}
else if (touched) {
guessButton.enabled = YES;
markButton.enabled = YES;
[self.view reloadInputViews];
NSLog(#"The action bar should now be visible");
}
I guess you have hooked self.view to UIView in Interface Builder.
In order to change UIView frame, in Interface Builder go to File Inspector, and uncheck Use Autolayout, then in the code change the frame.
Hope this helps.
You need to set adjust the frame in the viewDidAppear (not viewDidLoad).
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
CGRect frame = self.tableView.frame;
frame.origin.y += 100.0;
frame.size.height -= 100.0;
self.tableView.frame = frame;
}
Apparently this has something to do with table views within navigation controllers.
It is because of Auto Layout, however you could do it after Auto Layout done its work or change constraints of it. If you want to do it after auto layout add following.
- (void)viewDidAppear:(BOOL)animated {
dispatch_async(dispatch_get_main_queue(), ^{
box.frame = CGRectMake(20, 20, 100, 73)];
});
[self.view reloadInputViews] is generally used for FirstResponders. I Don't have much of an idea about your customview, but I think for just settings the frame you can use:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
viewFrame.frame=frame;//Frame Defined by you
[UIView commitAnimations];
can give better solution if i get a look at your custom view.

Adding a UIPickerView over a UITabBarController

I'm trying to have a UIPickerView slide from the bottom of the screen (over the top of a tab bar) but can't seem to get it to show up. The actual code for the animation is coming from one of Apple's example code projects (DateCell). I'm calling this code from the first view controller (FirstViewController.m) under the tab bar controller.
- (IBAction)showModePicker:(id)sender {
if (self.modePicker.superview == nil) {
[self.view.window addSubview:self.modePicker];
// size up the picker view to our screen and compute the start/end frame origin for our slide up animation
//
// compute the start frame
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
CGSize pickerSize = [self.modePicker sizeThatFits:CGSizeZero];
CGRect startRect = CGRectMake(0.0, screenRect.origin.y + screenRect.size.height, pickerSize.width, pickerSize.height);
self.modePicker.frame = startRect;
// compute the end frame
CGRect pickerRect = CGRectMake(0.0, screenRect.origin.y + screenRect.size.height - pickerSize.height, pickerSize.width, pickerSize.height);
// start the slide up animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
// we need to perform some post operations after the animation is complete
[UIView setAnimationDelegate:self];
self.modePicker.frame = pickerRect;
// shrink the vertical size to make room for the picker
CGRect newFrame = self.view.frame;
newFrame.size.height -= self.modePicker.frame.size.height;
self.view.frame = newFrame;
[UIView commitAnimations];
// add the "Done" button to the nav bar
self.navigationItem.rightBarButtonItem = self.doneButton;
}}
Whenever this action fires via a UIBarButtonItem that lives in a UINavigationBar (which is all under the FirstViewController) nothing happens. Can anyone please offer some advice?
Turns out I had missed the very important fact that the UIPickerView had been defined in Interface Builder as well... after doing that and connecting up the outlet everything is working!