2014-04-15 28 views
1

我需要在我的iOS应用中添加广告功能。广告屏幕会在一定的时间间隔后出现。我的整个只处于横向模式。当我试图在当前视图上添加视图时,它将以纵向模式显示视图而不是横向模式。我已经设置了视图帧即CGSizeMake(0,0, 568, 320)在横向模式下添加UIView

time = [NSTimer scheduledTimerWithTimeInterval:2.0f 
              target:self 
             selector:@selector(showfirstad) 
             userInfo:nil 
             repeats:YES]; 
-(void)showfirstad { 
    [[[[UIApplication sharedApplication] windows] lastObject] addSubview:firstad]; 
} 

看来这样subview appears in portrait mode

+0

@Agent我需要以横向模式而不是纵向模式显示视图。 –

+0

请发表一个'firstad'代码片段 – cyborg86pl

+0

@cyborg86pl我在xib中做了视图 –

回答

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

你的UIView以横向显示而其他应用程序显示在横向上的原因是你将UIView作为你窗口的子视图添加,而不是将它作为视图控制器视图的子视图添加。这将它放置在通过自动转换自动转换的视图层次结构之外。

应用程序的窗口对象通过查找具有相应视图控制器的最顶层子视图来协调自动旋转。设备旋转时,窗口调用shouldAutorotateToInterfaceOrientation:在此视图控制器上并根据需要转换其视图。如果您希望视图自动旋转,它必须是该视图层次结构的一部分。

所以不是[window addSubview:UIView];,这样做[self.view addSubview:UIView];

+0

但是我需要在最上面的控制器上添加视图。为此,我正在使用窗口而不是self.view –

0

我曾与旋转和autolayots同样的问题使用addSubview时:MyView的。 我设法通过使用标准容器控制器或直接将视图放置到故事板来解决此问题。 您可能只需添加将您的广告放入故事板屏幕中的视图,然后将隐藏属性设置为YES即可。然后,您可以在一段时间后将其更改为YES。