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
Related
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:
I have a navigation-based application where it is possible for the user to hide the status bar. This is remembered in a defaults setting.
It is mostly working, with the one exception that if the app is loaded (from scratch, not returned to after going back to the home screen) when the status bar should be visible, when it is toggled to invisible, the navigation bar does not move up to fill the screen. Toggling the setting after that moves the navigation bar up and down correctly, but still with the extra status bar-sized gap between the navigation bar and the top of the screen, or the status bar. If I return to the home screen and re-enter the application, this corrects itself. I therefore assume there is some method being called on the uinavigationcontroller upon return to the application that I need to call after my toggling of the status bar?
I have tried (I think) all combinations of the wantsfullscreenlayout property, I was setting it in the method below but it made no difference, so I ended up setting it (on the navigationcontroller) to NO in the nib.
Here is the code which toggles the status bar. This is in my application delegate, which has the navigationcontroller and window set up as outlets as per the template application.
if ([UIApplication sharedApplication].statusBarHidden != hideStatusBar)
{
[[UIApplication sharedApplication] setStatusBarHidden:hideStatusBar withAnimation:UIStatusBarAnimationSlide];
[UIView animateWithDuration:0.25 animations:^{
window.frame = [[UIScreen mainScreen] applicationFrame];
}];
}
else
{
window.frame = [[UIScreen mainScreen] applicationFrame];
}
Thanks for your help.
UPDATE
It seems, via NSLogging, that the problem lies in the frame of the UINavigationBar. So I have added the following code, which works and animates but I am not happy with! I don't feel this can be the "correct" way to do this. In most cases the extra code does nothing since the frame is already at (0,0), but in the one situation where it is incorrect, this gives the right result.
[navigationController.view setNeedsLayout];
CGRect navBarFrame;
UINavigationBar *navBar = nil;
for (UIView *subView in navigationController.view.subviews)
{
if ([subView isMemberOfClass:[UINavigationBar class]])
{
navBar = (UINavigationBar *)subView;
navBarFrame = navBar.frame;
navBarFrame.origin = CGPointMake(0,0);
break;
}
}
if ([UIApplication sharedApplication].statusBarHidden != hideStatusBar)
{
[[UIApplication sharedApplication] setStatusBarHidden:hideStatusBar withAnimation:UIStatusBarAnimationSlide];
[UIView animateWithDuration:0.25 animations:^{
window.frame = [[UIScreen mainScreen] applicationFrame];
navBar.frame = navBarFrame;
}];
}
else
{
window.frame = [[UIScreen mainScreen] applicationFrame];
}
The window always underlaps the status bar, so you shouldn't try to resize it.
Instead, resize the view of your window's root view controller to [[UIScreen mainScreen] applicationFrame].
the problem lies in the frame of the UINavigationBar. So I have added the following code, which works and animates but I am not happy with! I don't feel this can be the "correct" way to do this. In most cases the extra code does nothing since the frame is already at (0,0), but in the one situation where it is incorrect, this gives the right result.
[navigationController.view setNeedsLayout];
CGRect navBarFrame;
UINavigationBar *navBar = nil;
for (UIView *subView in navigationController.view.subviews)
{
if ([subView isMemberOfClass:[UINavigationBar class]])
{
navBar = (UINavigationBar *)subView;
navBarFrame = navBar.frame;
navBarFrame.origin = CGPointMake(0,0);
break;
}
}
if ([UIApplication sharedApplication].statusBarHidden != hideStatusBar)
{
[[UIApplication sharedApplication] setStatusBarHidden:hideStatusBar withAnimation:UIStatusBarAnimationSlide];
[UIView animateWithDuration:0.25 animations:^{
window.frame = [[UIScreen mainScreen] applicationFrame];
navBar.frame = navBarFrame;
}];
}
else
{
window.frame = [[UIScreen mainScreen] applicationFrame];
}
As far as I know if you hide the status bar your views automatically occupy the new space and you don't have to change the window frame manually. What happens if you try just this
if ([UIApplication sharedApplication].statusBarHidden != hideStatusBar)
{
[[UIApplication sharedApplication] setStatusBarHidden:hideStatusBar withAnimation:UIStatusBarAnimationSlide];
}
This is what solved it for me
Just a View:
[self.view setFrame: [self.view bounds]];
A view with a scroll view inside
[self.view setFrame: [self.view bounds]];
[self.theScroller setFrame: [self.view bounds]];
"theScroller is the name of my scrollview
I am trying to dismiss a view controller from bottom to top instead of the standard right to left transition. Is this at all possible? Here is the code I have so far:
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
CGRect endFrame = self.view.frame;
endFrame.origin.y = screenRect.origin.y - screenRect.size.height;
UIView *aView = [[self.view retain] autorelease];
[self.view.window addSubview:aView];
aView.frame = CGRectMake(0, 0, 320, 460);
[UIView animateWithDuration:0.5
animations:^{
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
aView.frame = endFrame;
}
completion:^(BOOL finished) {
[self dismissModalViewControllerAnimated:NO];
[aView removeFromSuperview];
}
];
This does result in a transition; but the previous view controller does not appear until after the animation since I can't dismiss until after it completes... any ideas?
Aah, when you presentModalViewController, it automatically hides the view behind. What you need to do is not present and remove, but add the view controllers view as a subview of the main view. Then you just animate the view offscreen, and removeFromSuperview in the competition handler.
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
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!