Widgets and Screen Rotation in IOS - ios

I have an ipad app using storyboards which is laid out in landscape mode. All the labels and and imageviews are centered in the storyboard. It looks great in landscape mode, but as soon as the tablet is rotated, the ipad redraws the widgets using the landscape screen parameters, rather than the portrait mode ones. How do I get the widgets to redraw when in the correct mode?

You can use auto resizing to redraw your view to Portait mode.
Or
You can use interfaceOrientation to set a exact position for the views which is not aligned in Portait mode something like the code below
Note: Change Widgets frame only if it is improper.
Learn to position your views using auto resizing... which is more appreciated.
Animate it to have good orientation feel.
For ios 6 use- (BOOL) shouldAutorotate
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (interfaceOrientation == UIInterfaceOrientationPortrait||
interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
//Code
dispatch_async(dispatch_get_main_queue(), ^{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
_btn1.frame = CGRectMake(190, 270, 137, 240);
_btn2.frame = CGRectMake(440, 270, 137, 240);
_btn3.frame = CGRectMake(300, 40, 137, 240);
[UIView commitAnimations];
});
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight||
interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
{
dispatch_async(dispatch_get_main_queue(), ^{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
_btn1.frame = CGRectMake(250, 180, 137, 240);
_btn2.frame = CGRectMake(640, 180, 137, 240);
_btn3.frame = CGRectMake(440, 40, 137, 240);
[UIView commitAnimations];
});
//Code
}
return YES;
}

Related

Moving UIImageView frame programmatically

I have UIView, in top of it i have an UIImageView, in bottom UICollectionView. My point is, when user swipe finger up, UIImageView frame should gently move out (disappear), and when user swipe down, it should again appear (with moving animation).
Actually, it worked until i set image to UIImageView. Old code (presented below) did it work just fine:
-(void)handleSwipeDown:(UISwipeGestureRecognizer *)recognizer {
NSLog(#"Swipe received.");
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
self.myCollectionView.frame = CGRectMake(0, 152, 320, 480);
[UIView commitAnimations];
}
And then:
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
NSLog(#"Swipe received.");
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
self.myCollectionView.frame=CGRectMake(0, 0, 320, 480);
[UIView commitAnimations];
}
After i set image:
UIImage *firstImage = [UIImage imageNamed:#"ipd1.jpg"];
self.imageSlideshow.image = firstImage;
Code above stop work. UIImage frame stay in same place.
How could i make it move ?
Any advice would be appreciated, thanks!
You should stop using the beginAnimations:context: and commitAnimations block. From Apple's documentation:
Use of this method is discouraged in iOS 4.0 and later. You should use the block-based animation methods to specify your animations instead.
Use this instead:
[UIView animateWithDuration:0.5 animations:^{
self.myCollectionView.frame = CGRectMake(0, 152, 320, 480);
}];
Just replace your entire swipeDown method with the above.

Animation does not seem to work - move view from one position to another on [self dismissViewController

I have a view currently on the screen and I would like to move it off the screen during an animation. Its current position is: CGRectMake(0, 168, 320, 50); and I would like to move it to y postion 218.
Here is my code:
[UIView beginAnimations:Nil context:NULL];
[UIView setAnimationDuration:0.3];
self.optionsView.frame = CGRectMake(0, 218, 320, 50);
[UIView commitAnimations];
Thanks in advance. Nothing is happening, the view isnt moving off screen.
Edit: When the app launches the view is off screen, i then call:
[UIView beginAnimations:Nil context:NULL];
[UIView setAnimationDuration:0.3];
self.optionsView.frame = CGRectMake(0, 168, 320, 50);
[UIView commitAnimations];
to bring the view into the main view. The problem I have is making the view go back off screen again with:
[UIView beginAnimations:Nil context:NULL];
[UIView setAnimationDuration:0.3];
self.optionsView.frame = CGRectMake(0, 218, 320, 50);
[UIView commitAnimations];
It must be noted that in order to get the view to be off screen on app launch I call:
-(void) viewDidLayoutSubviews{
self.optionsView.frame = CGRectMake(0, 218, 320, 50);
}
Update: I am also using the camera, i think dismissing the camera is affecting the layouts
I've animated the position of views as follows:
// 'frame' variable previously declared
// 'completion' block previously declared
[UIView animateWithDuration:0.3f
delay:0.0f
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[_someView setFrame:frame];
}
completion:^(BOOL finished){
if (completion) {
completion();
}
}];
Have you tried it that way?
As Martin Koles has said, the block-based approach to view animation is preferred. The following is in the reference doc for UIView:
Use of the methods in this section is discouraged in iOS 4 and later.
Use the block-based animation methods instead.
https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/UIView/UIView.html
Use block instead:
[UIView transitionWithView:self.view duration:0.3 options:UIViewAnimationOptionCurveEaseInOut animations:^{
// final view elements attributes goes here
} completion:nil];
This is the modern way of doing animations. You can even specify a completion block, something that should happed after the animation is done, if needed.
Try using this
[UIView animateWithDuration:0.3 delay:0 options: UIViewAnimationOptionCurveLinear
animations:^{
self.optionsView.frame = CGRectMake(0, 218, 320, 50);
} completion:nil];
Worked fine with me.
Make sure the view is connected in the xib to its proper object.

UIView Will Not Resize

My storyboard elements are subviews of containerView and containerView is a subview of the main view. I am trying to resize the height of my container view when an ad is available to show but I cannot get that to work. I am able to offset the view up, but I am not able to resize it. I have seen quite a bit a posts about this, but all suggestions I've read basically say to confirm Autolayout is not checked. I have confirmed that Autolayout is not checked in each element of my storyboard but still am not having success with this. The ad banner (placed just off screen at [0, 480] pops up nicely from the bottom just like I want it to, but it covers up my storyboard elements which is just plain UNACCEPTABLE. I will not stand for this! I need some help guys and gals…Please see code below:
-(void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (_bannerIsVisible == NO)
{
NSLog(#"Ad Loaded");
[UIView beginAnimations:#"animateAdbannerOn" context:nil];
//_containerView.frame = CGRectOffset(_containerView.frame, 0, -50);
_containerView.frame = CGRectMake(_containerView.frame.origin.x,
_containerView.frame.origin.y,
_containerView.frame.size.width,
_containerView.frame.size.height-50);
banner.frame = CGRectOffset(banner.frame, 0, -50);
[UIView commitAnimations];
_bannerIsVisible = YES;
}
}
Try it like this:
-(void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (_bannerIsVisible == NO)
{
NSLog(#"Ad Loaded");
[UIView beginAnimations:#"animateAdbannerOn" context:nil];
//_containerView.frame = CGRectOffset(_containerView.frame, 0, -50);
CGRect frame = CGRectMake(_containerView.frame.origin.x,
_containerView.frame.origin.y,
_containerView.frame.size.width,
_containerView.frame.size.height-50);
[_containerView setFrame:frame];
CGRect frame2 = banner.frame;
frame2.origin.x = 0;
frame2.origin.y = -50;
[banner setFrame:frame2];
[UIView commitAnimations];
_bannerIsVisible = YES;
}
}
You have just changed the frame of your containerView. This does not in any way change the frames of the subviews of containerView. You either have to make containerView a scrollview or change the frames of every component.

changing UIWebView frame on device rotation

I'm developing an app that is locked to portrait mode. However one view is acting as a full screen 'player' for various media types.
I'm using a UIView subclass that has a UIWebview subview to play the media.
This is presented full screen on top of the viewing stack. My problem arises in making only this view rotate for landscape content - e.g. videos & pdfs.
To do the rotation I'm setting an observer in the init method:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(deviceRotated:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
and rotating as so:
- (void)deviceRotated:(NSNotification *)notification {
UIDeviceOrientation orientation = [[notification object] orientation];
if (orientation == UIDeviceOrientationLandscapeLeft) {
[UIView animateWithDuration:0.6f delay:0.0 options:UIViewAnimationCurveEaseInOut
animations:^{ [self.webView setTransform:CGAffineTransformMakeRotation(M_PI / 2.0)]; }
completion:nil];
}
else if (orientation == UIDeviceOrientationLandscapeRight) {
[UIView animateWithDuration:0.6f delay:0.0 options:UIViewAnimationCurveEaseInOut
animations:^{ [self.webView setTransform:CGAffineTransformMakeRotation(M_PI / -2.0)]; }
completion:nil];
}
else if (orientation == UIDeviceOrientationPortraitUpsideDown) {
[UIView animateWithDuration:0.6f delay:0.0 options:UIViewAnimationCurveEaseInOut
animations:^{ [self.webView setTransform:CGAffineTransformMakeRotation(M_PI)]; }
completion:nil];
}
else if (orientation == UIDeviceOrientationPortrait) {
[UIView animateWithDuration:0.6f delay:0.0 options:UIViewAnimationCurveEaseInOut
animations:^{ [self.webView setTransform:CGAffineTransformMakeRotation(0.0)]; }
completion:nil];
}
}
I know I need to alter the frame size for self.webView when rotating, but I am getting strange results.
I've tried setting to container view as a large 'square' with length [[UIScreen mainscreen] bounds].size.height and placing the web view in the middle, but it moves about inconsistantly - it seems as if the width & height values are getting set from the dimensons before the rotation.
I've also tried setting autoResizingMask flexible height & width, but as the container view is off the screen this stretches the web view offscreen also.

UIView disappears after orientation change

I have the current problem:
From an NSObject i'm creating / loading and animating a view:
[UIView beginAnimations:#"slidein" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)];
[content.view setAlpha:1.0];
for (UIView *view in content.view.subviews) {
[view setAlpha:1];
}
[content.view setFrame:CGRectMake(0, 0, self.contentEnclosure.size.width, self.contentEnclosure.size.height)];
[UIView commitAnimations];
My problem is that, when orientation changes, my animated view disappears. If i make my view appear again, the orientation settings (frame size) are correct, so i'm guessing, that the resizing mask is applied correctly, i just don't understand what needs to be done, for my view not to disappear.
Use NSLog and check the new self.view.frame.origin.x and y respectively after the orientation.
Then set the frame according to the requirement.
Also, check for resizing mask. Try using UIViewAutoresizingFlexibleRightMargin/LeftMargin to suit your requirements.

Resources