Question

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 subview appears in portrait mode.

Was it helpful?

Solution

_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];

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top