2012-03-02 73 views
1

我正在构建一个monoTouch-iPad应用程序,并且由于启动界面方向而陷入困境。以正确的方向以编程方式显示UIView的最佳做法

一个问题是,当应用程序启动时UIDevice.CurrentDevice.Orientation总是返回Unknown。你如何决定你的应用程序在哪个方向启动?我现在发现的所有属性都只返回肖像模式,未知模式或肖像模式的帧大小 - 即使它是横向模式。

我还创建了两个UIViews(一个用于横向,一个用于纵向),现在在UIViewController的WillRotate方法中更改它们。但我的代码:

if(toInterfaceOrientation==UIInterfaceOrientation.LandscapeLeft || toInterfaceOrientation==UIInterfaceOrientation.LandscapeRight){ 

     _scrollView.RemoveFromSuperview(); 
     this.View.Add (_scrollViewLandscape); 
     }else{ 
     _scrollViewLandscape.RemoveFromSuperview(); 
     this.View.Add (_scrollView); 
} 

旋转屏幕时会产生短而难看的“闪烁” - 至少在模拟器中。

是否有布置视图的最佳做法?我知道ShouldAutorotateToInterfaceOrientation,但这对我不起作用,因为我正在做很多所有者绘制的东西,这些东西在自动修复后会被破坏(see my other question)。

我真的很感谢没有使用Interface-Builder的解决方案,因为我现在正在做所有的代码。

UPDATE:短描写的特征我想达到的目标: AppStart的 - >知道正确Framsize(1024,748或768,1004) - >添加我的自定义在正确的框架尺寸

UPDATE2观点:简单和基本的代码片段

public override void ViewDidLoad() 
    { 
     base.ViewDidLoad();    
     Console.WriteLine (this.InterfaceOrientation); 
    } 

返回肖像。即使模拟器处于横向模式。

回答

2

内UIViewController中可以只检查InterfaceOrientation

public override void ViewDidLoad() 
{ 
    if (this.InterfaceOrientation == UIInterfaceOrientation.Portrait 
     || this.InterfaceOrientation == UIInterfaceOrientation.PortraitUpsideDown) 
    { 
     // portrait 
    } 
    else 
    { 
     // landsacpe 
    } 
} 

,但我真的建议使用View.AutoresizingMask或压倒一切的LayoutSubviews,既让所有的转换真的顺利

更新:使用AutoresizingMask

public override void ViewDidLoad() 
{ 
    UIView view = new CustomView(View.Bounds); 
    view.AutoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth; 
    View.AddSubview(view); 
} 

U PDATE:覆盖LayoutSubviews

LayoutSubviews被称为每次大小的变化

public class CustomView : UIView 
{ 
    public override void LayoutSubviews() 
    { 
     //layout your view with your own logic using the new values of Bounds.Width and Bounds.Height 
    } 
} 
+0

很抱歉,但在应用程序启动'this.InterfaceOrientation'返回'Portrait'即使模拟器是在横向模式。 – basti 2012-03-02 11:00:35

+0

如何正确使用拥有所有视图的AutoresizingMask?重写LayoutSubviews有什么好处? – basti 2012-03-02 11:03:10

+0

我无法使用Autoresizing Masks工作,但我应该如何在LayoutSubviews中工作?借助转换或更换整个视图? – basti 2012-03-02 12:04:12