简体   繁体   English

在横向模式下添加UIView

[英]Add UIView in Landscape mode

I need to add the ads functionality in my iOS App. 我需要在iOS应用中添加广告功能。 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 ie CGSizeMake(0,0, 568, 320) 我设置了视图帧,即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. 您的UIView以纵向显示而其他应用程序以横向显示的原因是您将UIView添加为窗口的子视图,而不是将其添加为视图控制器视图的子视图。 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. 在设备旋转时,窗口调用shouldAutorotateToInterfaceOrientation:在此视图控制器上并根据需要转换其视图。 If you want your view to autorotate, it must be a part of this view's hierarchy. 如果您希望视图自动旋转,则它必须是此视图层次结构的一部分。

So instead of [window addSubview:UIView]; 而不是[window addSubview:UIView]; , do something like [self.view addSubview:UIView]; ,做一些像[self.view addSubview:UIView];

I had the same issues with rotation and autolayots when used addSubview:myView. 使用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. 您可以添加视图,将广告放入故事板中的屏幕,然后将隐藏属性设置为YES。 Then you can change it to YES after some time. 然后你可以在一段时间后将其改为YES。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM