Subview is not oriented properly - ios

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.

Related

UIView as subview orientation frame issue

I have taken UIView as subview in View Controller
#property(nonatomic,retain) IBOutlet UIView *subview;
then I am adding custom UIView class in subview like this ,
- (void)showdetail
{
CGRect frameToset = CGRectMake(0, 0, self.subview.frame.size.width,self.subview.frame.size.height) ;
customUIViewPaging *demoView = [[customUIViewPaging alloc] initWithFrame:frameToset];
... // so on
[self.subview addSubview:demoView];
}
In portrait mode it works perfectly but in landscape I am updating the frame like this,
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[self showdetail];
}
then after it works perfectly in landscape and portrait then I am pushing a another view from this view and landscaping Pushed view after back coming in this view then frame size not getting perfectly (Subview frame not updating the frames while orientation when coming back from another view)
I have tried to put needsUpdateConstraints but it seems not working
[self.subview needsUpdateConstraints];
Check this example
I am not going to Autolayout direction as you want correct frame when view appears ... If you want correct width than call it -(void)viewDidLayoutSubviews{..}
-(void)viewDidLayoutSubviews{
NSLog(#"%f",self.subview.frame.size.width); //here you get correct width
}
now the other problem is you are adding subviews everytime when screen rotates or this method calls .. so better to prevent that by removing previous view...

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

Add UIView in Landscape mode

I need to add the ads functionality in my iOS App. And ads screen would appear after some time interval. My whole is in Landscape mode only. When I tried to add the view on current view then it shows the views in portrait mode not in landscape mode. I have set the view frame i.e. CGSizeMake(0,0, 568, 320)
time = [NSTimer scheduledTimerWithTimeInterval:2.0f
target:self
selector:#selector(showfirstad)
userInfo:nil
repeats:YES];
-(void)showfirstad {
[[[[UIApplication sharedApplication] windows] lastObject] addSubview:firstad];
}
It appears like this .
_window = [UIApplication sharedApplication].keyWindow;
if (!_window) _window = [[UIApplication sharedApplication].windows objectAtIndex:0];
UIInterfaceOrientation orientation = self.window.rootViewController.interfaceOrientation;
// Set appropriate view frame (it won't be autosized by addSubview:)
CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
if (UIInterfaceOrientationIsLandscape(orientation))
{
// Need to flip the X-Y coordinates for landscape
self.view_login.frame = CGRectMake(appFrame.origin.y, appFrame.origin.x, appFrame.size.height, appFrame.size.width+20);
else
{
self.view_login.frame = appFrame;
}
[[[_window subviews] objectAtIndex:0] addSubview:self.view_login];
The reason your UIView gets displayed in portrait orientation while the rest of your app gets displayed in landscape is because you are adding the UIView as a subview of your window rather than adding it as a subview of a view controller's view. This places it outside of the view hierarchy that gets transformed automatically through autorotation.
The app's window object coordinates autorotation by finding its topmost subview that has a corresponding view controller. Upon device rotation, the window calls shouldAutorotateToInterfaceOrientation: on this view controller and transforms its view as appropriate. If you want your view to autorotate, it must be a part of this view's hierarchy.
So instead of [window addSubview:UIView];, do something like [self.view addSubview:UIView];
I had the same issues with rotation and autolayots when used addSubview:myView.
I managed to solve this problem by using standard container controllers or placing views directly to storyboard.
You can probably just add the view that will keep your ad into the screen in storyboard and then set hidden property to YES. Then you can change it to YES after some time.

Landscape xib appears as portrait

I have a view controller and separate nib files for portrait and landscape. On rotating, I load the respective nib. The methods
shouldAutorotateToInterfaceOrientation and willRotateToInterfaceOrientation
get called and the nib does change.
The problem:
the landscape nib does not appear as landscape, but portrait! The status bar is
correctly rotated and appears on the top:
(Sorry, couldn't paste the image, because my account is new. The screenshot is in
landscape, with a landscape status bar, but a landscape view shown as portrait.)
One would think the problem lies in not setting the orientation as Landscape in IB Simulated metrics for the view, but I've done that and the view appears as landscape in IB. (I don't think it would even be possible to create a rotated button like that if the view was portrait.) Besides these two nibs I have a mainwindow.xib, which contains the app delegate, window and view controller objects.
EDIT: I realized that the views are actually rotating, when they should "stay up". It's like there's an extra transformation. When I rotate the phone 90° right, the landscape xib is displayed rotated 90° right. When I rotate the phone another 90° right, the portrait xib is displayed upside down. The status bar is always correctly displayed at the top.
EDIT2: Using
self.view.transform = CGAffineTransformMakeRotation((M_PI * (90) / 180.0));
in willRotateToInterfaceOrientation I can rotate the view to landscape left (and to any orientation I want), so I can use that as a workaround. However, I have other projects, where the view rotates automatically and doesn't require the use of CGAffineTransformMakeRotation. It's like something is preventing the automatic rotation here.
Are you adding the view loaded from nib as subView? If Only the status bar is rotating it means your previous view is hung while releasing the view and adding the new view.Can you tell how are you adding the view loaded from xib to the SuperView.
Make sure you are releasing the previous view correctly while loading the other view,put NSLOG in dealloc of the views and check whether the view is getting released completely.
I had done something similar to this only instead of making an nib file separately I just added two subviews to the main nib as prtraitView and Landscape View
and switched them as follows
In viewDidAppear method
-(void)viewDidAppear:(BOOL)animated{
if(UIDeviceOrientationIsPortrait(self.interfaceOrientation))
{
self.portraitVIew.frame=self.view.bounds;
self.portraitVIew.frame=self.view.frame;
[self.view addSubview:self.portraitVIew];
}else{
self.landscapeView.frame=self.view.frame;
[self.view addSubview:self.landscapeView];
}
self.view.autoresizesSubviews = YES;
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(deviceOrientationDidChangeNotification:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
}
and then Implemented deviceOrientationDidChangeNotification as follows
- (void)deviceOrientationDidChangeNotification:(NSNotification*)note
{
if ([[UIDevice currentDevice] userInterfaceIdiom]==UIUserInterfaceIdiomPad) {
}else{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if(UIDeviceOrientationIsLandscape(self.interfaceOrientation))
{
self.landscapeView.hidden=NO;
self.landscapeView.frame=self.view.frame;
[self.portraitVIew removeFromSuperview];
[self.view addSubview:self.landscapeView];
}else {
self.landscapeView.hidden=YES;
self.portraitVIew.frame=self.view.frame;
NSLog(#"Portrait");
[self.view addSubview:self.portraitVIew];
}
}
}
It worked very well for me..

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

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.

Resources