简体   繁体   English

以编程方式为iPhone 5更改UIView布局

[英]Change UIView layout programmatically for iPhone 5

I am trying to change the layout of my UIView programmatically, and not by using Auto Layout, as I am trying to deploy this app for iOS 4 and up. 我正在尝试以编程方式而不是通过使用“自动布局”来更改UIView的布局,因为我正在尝试为iOS 4及更高版本部署此应用。 I basically set my view up with the screen height at 455(iPhone 5)(I know the actual screen size is 568 points, but I have a tab bar on the bottom so I am assuming the size shown in IB has taken that into account). 我基本上将屏幕高度设置为455(iPhone 5)(我知道实际屏幕大小为568点,但我在底部有一个标签栏,所以我假设IB中显示的大小已考虑在内) )。 I recorded the origin of all of my buttons and labels relative to the longer screen size and programmatically changed them to how I preferred them. 我记录了所有按钮和标签相对于较长屏幕尺寸的来源,并以编程方式将它们更改为我喜欢的方式。 The problem is, some of the buttons and labels are going off the screen now, so I think I have to perform some kind of conversion to keep them in bounds, but I am not sure what. 问题是,一些按钮和标签现在不在屏幕上了,所以我认为我必须执行某种转换以使它们保持界限,但是我不确定是什么。 See the pictures attached below: 请参阅下面的图片:

Image before my initMethod is called to change button and label origins 在调用initMethod更改按钮和标签原点之前的图像 Image after my initMethod is called to change the origin 调用initMethod更改原点后的图像 View properties in XCode for the old screen size 查看XCode中旧屏幕尺寸的属性 This is how I want the view to Look. 这就是我想要的外观。 I moved the buttons around and recorded the origin of each. 我移动了按钮,并记录了每个按钮的来源。 在此处输入图片说明 Picture of my init method in viewWillAppear. 我在viewWillAppear中的init方法的图片。 Notice how the frame of the label is zero. 请注意标签的框架如何为零。 在此处输入图片说明 Here is the code I used: 这是我使用的代码:

-(void)initView
{
    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    if (screenBounds.size.height == 480) {
        //iphone 4,4s
        //done
    }else if(screenBounds.size.height == 568) {
        //iphone 5
        CGRect frame;
        //Static Professor label
        frame = [staticProfessorLabel frame];
        frame.origin.x = 48;
        frame.origin.y = 218;
        [staticProfessorLabel setFrame:frame];
        //Professor Label
        frame = [professorLabel frame];
        frame.origin.x = 192;
        frame.origin.y = 218;
        [professorLabel setFrame:frame];
        //show button
        frame = [showProfessorButton frame];
        frame.origin.x = 67;
        frame.origin.y = 258;
        [showProfessorButton setFrame:frame];
        //clear proff button
        frame = [clearProfessor frame];
        frame.origin.x = 240;
        frame.origin.y = 258;
        //note label
        frame = [bottomNote frame];
        frame.origin.x = 160;
        frame.origin.y = 310;
        [bottomNote setFrame:frame];
        //search button
        frame = [searchButton frame];
        frame.origin.x = 158;
        frame.origin.y = 424;
        [searchButton setFrame:frame];
        //spinner
        frame = [actIndicator frame];
        frame.origin.x = 266;
        frame.origin.y = 424;
        [actIndicator setFrame:frame];
    }
}

Also, I am calling this in the viewDidAppear method because when I call it in viewDidLoad, none of the buttons and labels give me a valid frame(I am guessing they are not initialized yet). 另外,我在viewDidAppear方法中调用此方法是因为当我在viewDidLoad中调用它时,没有按钮和标签给我有效的框架(我想它们尚未初始化)。

Your Xcode screenshots show that your storyboard contains constraints, so we can tell that your storyboard has autolayout turned on. 您的Xcode屏幕截图显示您的情节提要板包含约束,因此我们可以判断您的情节提要板已启用自动布局。 If you want to set the view frames directly in code, you have to turn off autolayout. 如果要直接在代码中设置视图框架,则必须关闭自动布局。

Take a look at my answer here if you need help turning off autolayout on your storyboard. 如果您需要关闭故事板上的自动版式的帮助,请在这里查看我的答案

Your hard-coded coordinates are simply wrong. 您的硬编码坐标是完全错误的。 Just double-check your values. 只需仔细检查您的价值观即可。

For example, bottomNote starts at x 160, which is clearly too far to the right. 例如, bottomNotex 160开始,这显然在右边。

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

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