【问题标题】:Resizing iPhone keywindow's main view调整 iPhone 键窗口主视图的大小
【发布时间】:2009-11-23 09:40:27
【问题描述】:

编辑 2:当我在顶部没有状态栏的情况下启动应用程序时,一切都按计划运行。使用状态栏,我无法让视图按照我的意愿行事。看起来 UINavigationController 通过减去状态栏的 20 像素来不断调整内容视图的大小。我不知道。

我创建了一个简单的基于 UINavigationController 的应用程序。此导航控制器中的根视图是 UITableView。在某个时间,我想从底部滑入一个 80 像素高的视图。顶部的整个视图(由 UINavigationController 控制的视图)应调整大小并缩小 80 像素,以便为新的底部视图腾出空间。

这基本上是我用来重新定位视图的代码:

-(void)showTeaser {
float adHeight = 80;

[adView setFrame:CGRectMake(0.0,self.navigationController.view.bounds.size.height, 320.0, 80.0)];
[[[UIApplication sharedApplication] keyWindow] addSubview:adView];

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[adView setAlpha:1.0];
[adView setFrame:CGRectMake(0.0,self.navigationController.view.bounds.size.height-adHeight, 320.0, 80.0)];
[self.navigationController.view setFrame:CGRectMake(0.0,0.0, 320.0, self.navigationController.view.bounds.size.height-adHeight)]; 

[self.view setFrame:CGRectMake(0.0, 0, 320.0, self.view.bounds.size.height-adHeight)]; 

[UIView commitAnimations]; }

我降低了导航栏的 alpha,将 UITableviewController 的视图设置为红色。新视图为紫色。

这就是发生的事情。第一张截图初始状态。一切看起来都很正常。第二个屏幕截图显示了更改帧后的状态。 UITableviewController 的视图总是被推到导航栏下方 20 像素。此外,如果我尝试向 keywindow 添加更多视图,它们最终总是比我预期的高 20 像素。它几乎看起来像键窗口(减去导航栏)被推高了 20 像素。

编辑 1:无论我调整视图的大小,它始终是 20 像素。

向键窗口添加视图是否会出错?我不应该这样做吗?

alt text http://www.hans-schneider.de/iphone-seo/1.pngalt text http://www.hans-schneider.de/iphone-seo/2.png

【问题讨论】:

  • 我已经使用了我的 tableviewcontrollers 视图的自动调整大小掩码,但这并没有改变基本问题。

标签: iphone objective-c


【解决方案1】:

为了解决这个问题,我将UINavigationController 的视图设为UIView 的子视图,并手动设置了`UINavigationController' 的视图边界。

//outerView is a UIView defined in the interface
outerView = [[UIView alloc] initWithFrame:CGRectMake( 0.0, 0.0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);]; 

//mainNavigationController is a UINavigationController defined in the interface
//rootViewController is a UIViewController (or inherited class) defined in the interface and instanced before this code
mainNavigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];

//set the frame for the view of the navigation controller - 20 is due to the status bar
mainNavigationController.view.frame = CGRectMake( 0.0, 20.0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 20);

然后,当我去调整大小时,我调整父“UIView”而不是“UINavigationController”视图的大小。

//change the outer view's frame to resize everything
//adHeight is a float defined earlier
outerView.frame = CGRectMake( 0.0, 20.0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 20 - adHeight);

这在动画序列中效果很好。

编辑 2011-05-12: 我更新了 outerView 框架以填充屏幕。这必须设置为允许触摸事件。

【讨论】:

    【解决方案2】:

    您是否尝试过使用 tableview 的 transform 属性而不是手动更改它的框架?它可能会更好,因为框架取决于原点,而您只想更改它的大小。

    【讨论】:

      猜你喜欢
      • 2013-07-26
      • 2017-01-20
      • 2011-03-18
      • 1970-01-01
      • 2016-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多