position of UIScrollView getting changed when Orientation is changing in ipad twice - ios

The scrollview is changing its position automatically when I switch to landscape and portrait modes more than one time while scrolling the scrollview.
What else I have to do...to retain its position in the same manner as it was loaded for the first time. I would like to scroll horizontally all the time in landscape mode and always vertical in portrait mode.
The below code is just I'm using:
UIScrollView * myBookScrollView = [[MyBooksScrollView alloc] init];
myBookScrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
myBookScrollView.clipsToBounds = YES;
myBookScrollView.scrollEnabled = YES;
[self.view addSubview:myBookScrollView];

Did you try to set it's autoresizingMask? Setting it to UIViewAutoresizingFlexibleWidth might help.

Related

iAd frame doesn't rotate to landscape using setAutoresizingMask on iOS8

I have created an iAd frame right on top of the tab bar and I am using setAutoresizingMask to keep it in the right place when rotating the screen. Here is my code in viewDidLoad:
iAd = [[ADBannerView alloc] init];
bannerIsVisible=NO;
iAd.delegate=self;
[iAd setAutoresizingMask: UIViewAutoresizingFlexibleTopMargin
| UIViewAutoresizingFlexibleWidth];
// set iAd frame
iAd.frame = CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, 50);
[self.view addSubview:iAd];
This will create the banner just below the screen, so it will appear when the ad is loaded using the bannerViewDidLoadAd: delegate method and going back using the didFailToReceiveAdWithError method.
This works perfectly on iOS 6 and iOS7.
On iOS8 the banner appears in the right place when portrait, but when I rotate the screen to landscape, the banner appears around 10 points over the right place. Is like it doesn't take into consideration that the height of the frame is smaller when landscape.
Did something change in iOS8 about setAutoresizingMask?
Thanks

Objective C iOS UISegmentedControl changing position unexpectedly

I have a UISegmented control that I need to change the position of whenever the view goes into a landscape view. My code works, but for some very strange reason whenever I try to select the segmented control, it moves back to its portrait position which is off of the screen. This is not happening with buttons that I am using the same method to move them with.
Code:
// handle landscape view
-(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
CGSize size = [UIScreen mainScreen].bounds.size;
UIApplication *app = [UIApplication sharedApplication];
UIInterfaceOrientation orientation = [app statusBarOrientation];
if (UIInterfaceOrientationIsLandscape(orientation)) size = CGSizeMake(size.height, size.width);
if (!app.statusBarHidden) size.height -= MIN(app.statusBarFrame.size.width, app.statusBarFrame.size.height);
if (UIInterfaceOrientationIsLandscape(orientation))
{
// CGRects of segmented controls
CGRect modeFrame = [self offAutoHeatCool].frame;
CGRect fanModeFrame = [self fanMode].frame;
modeFrame.origin.y = someRelativeInt;
fanModeFrame.origin.y = someRelativeInt;
modeFrame.origin.x = someRelativeInt;
fanModeFrame.origin.x = someRelativeInt;
[[self modeLabel] setFrame:modeLabelFrame];
[[self offAutoHeatCool] setFrame:modeFrame];
}
}
Edit:
Please keep in mind that I need to move certain UI elements from being above or beneath each other to being on the side of relative UI elements. I'm not certain if you can do this using auto layout, and I would prefer to do so programmatically anyways.
The proper way to deal with views moving around resizing (or orientation changing) parent views is to use auto layout. It will save you a lot of hassle once you begin using it. Take a look at the documentation here:
https://developer.apple.com/library/ios/documentation/userexperience/conceptual/AutolayoutPG/Introduction/Introduction.html
Hope this helps!
I figured it out. All I had to do was disable auto layout for that view. To do this I selected the view controller in the storyboard, selected the file inspector, and unchecked "Use Auto Layout" under "Interface Builder Options".

iOS Truncated Views while rotating, have to reset view.bounds

I've been adding support for rotation for an app recently and it has been a pain. One thing I'm finding that's fairly consistently annoying is that one of my views shifts up by about 50 pixels or so everytime I rotate between my landscape and portrait mode.
My landscape mode is not actually the same view controller; I push a viewcontroller when I rotate. However, when I rotate back, I have to reset the portrait's view.bounds or else my view ends up shifting upwards.
So in my rotation code, I have to do this:
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
UIInterfaceOrientation toOrientation = self.interfaceOrientation;
if ( self.tabBarController.view.subviews.count >= 2 )
{
if(toOrientation == UIInterfaceOrientationPortrait)
{
self.tabBarController.tabBar.hidden = NO;
self.navigationController.navigationBar.hidden = NO;
CGFloat height = [[UIScreen mainScreen] bounds].size.height;
CGFloat width = [[UIScreen mainScreen] bounds].size.width;
self.view.bounds = CGRectMake(0, -55, width, height);
}
}
}
Surely this can't be right. In my app, there is a navbar and the standard status bar (batt life, reception, etc) occupying the top of my app. But...it seems like my view is slipping too upwards unless I set the y coordinate origin to be negative (which makes no sense!).
What's happening?
In my app, I hide the tabbar and navbar when I go to landscape mode. The statements to make the bars hidden are written into the portrait view's viewcontroller's code.
When I transition back from landscape mode to portrait mode, the landscape viewcontroller gets popped and I get the weird shifted views. Turns out this was caused by the order in which the tab/nav bar un-hiding statements.
My tab/nav bar un-hiding statements were in the portrait viewcontroller, so they were called too late. After moving the tab/nav un-hiding statements to the rotation code in the landscape viewcontroller (rather than the portrait's viewcontroller), my problem disappeared.

Subview is not oriented properly

I have created a subview which is used as a HUD in an iOS app. The app supports portrait and landscape display orientations.
The subview appears in portrait mode sometimes even if the mode is landscape. The subview is added by these lines:
self.infoHUDViewController = [[[InfoHUDViewController alloc] initWithNibName:#"InfoHUD" bundle:nil] autorelease];
[self.view addSubview:self.infoHUDViewController.view];
So it works very well if the subview is added in the viewDidLoad method. However, it does not work in landscape mode if the view is added in an IBAction method which responds to a button press. The subview appears as if in portrait mode (it appears 90° rotated, fills only the half screen width, and extends beyond the screen bounds).
What is really odd: In the init method of the subview's view controller the display orientation is correctly set to landscape. Am I missing something here?
I found a fix for this problem.
I added
if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {
self.view.frame = CGRectMake(0.0f, 0.0f, 480.0f, 320.0f);
} else {
self.view.frame = CGRectMake(0.0f, 0.0f, 320.0f, 480.0f);
}
to the viewDidLoad method. Now everything works as excepted in all situations.

How to handle different scrollView for orientation change in iPad?

I have a scroll view with 2 different subviews for two different orientation.
In portait mode, it shows a table view and in portait it shows the UIImageView.
I want to know the ideal way to handle the orientation.
I am having glitches with my current way of handling
1.Load a view,and add scrollview as a subview to the root view.
2.Change orientation,
3.Change the frame and contents of the scrollview.
4.Add this modified scrollview as subview again.
But, every time I change orientation,the new view gets added on top of the previous one.
If you want to have two different views for different orientations, you need to switch them while orientation is changing.
When you go from portrait to landscape, you have to remove UIImageView and add scrollview (and vice versa)
[myImageView removeFromSuperview];
[self.view addSubview:scrollView];
You can also check if view doesn't have a superview to be sure that you have to add it:
if(scrollView.superview == nil)
[self.view addSubview:scrollView];
As you have already added scrollView as subView so you do not need to add it as subView again.
Just change the frame and contentSize of scrollView.
for Views do as
[scrollView.subView removeFromSuperView]
[scrollView addSubView:view1]; // or add view2

Resources