模式更改时如何在视图中心自动设置文本字段和按钮?

Posted

技术标签:

【中文标题】模式更改时如何在视图中心自动设置文本字段和按钮?【英文标题】:How set text field and button at center of view automatically when mode change? 【发布时间】:2011-07-13 08:37:53 【问题描述】:

当我更改横向或纵向视图模式时,我想在 UIView 的中心自动设置文本字段、uibutton、标签和图像视图。我的意思是,当我更改方向时,文本字段、按钮和图像会自动设置在视图中心。我如何使用界面生成器或动态设置它们?

【问题讨论】:

【参考方案1】:

用这个方法写代码

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

    return YES;



-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation

    if (fromInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || fromInterfaceOrientation == UIInterfaceOrientationLandscapeRight )
    
        //write your code specific to orientation
    
    else if(fromInterfaceOrientation == UIInterfaceOrientationPortrait || fromInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
    
            //write your code specific to orientation
    


【讨论】:

【参考方案2】:

您可以通过编程方式设置视图的center 属性。当您检测到界面方向更改时执行此操作。

【讨论】:

【参考方案3】:

你必须为此构造两个不同的框架。一个用于普通视图,另一个用于横向视图。然后在 interFaceOrientationChange 方法中,您必须设置新框架。

【讨论】:

【参考方案4】:

使用 button.center = view.center 属性。

【讨论】:

【参考方案5】:

在 UIView 上使用 autoresizing 属性并进行相应设置...最方便的方法

【讨论】:

以上是关于模式更改时如何在视图中心自动设置文本字段和按钮?的主要内容,如果未能解决你的问题,请参考以下文章