objective-c - 以编程方式更改 iPhone 5 的 UIView 布局

标签 objective-c ios cocoa-touch uiview resize

我正在尝试以编程方式更改 UIView 的布局,而不是使用自动布局,因为我正在尝试为 iOS 4 及更高版本部署此应用程序。我基本上将屏幕高度设置为 455(iPhone 5)(我知道实际屏幕尺寸为 568 点,但底部有一个标签栏,所以我假设 IB 中显示的尺寸已考虑到这一点).我记录了所有按钮和标签相对于较长屏幕尺寸的来源,并以编程方式将它们更改为我喜欢的方式。问题是,一些按钮和标签现在从屏幕上消失了,所以我想我必须执行某种转换以将它们限制在边界内,但我不确定是什么。请参阅下面的图片:

调用我的 initMethod 以更改按钮和标签原点之前的图像 调用我的 initMethod 以更改原点后的图像 在 XCode 中查看旧屏幕尺寸的属性 这就是我希望 View 的外观。我四处移动按钮并记录每个按钮的来源。 enter image description here 我的 init 方法在 viewWillAppear 中的图片。注意标签的框架是如何为零的。 enter image description here 这是我使用的代码:

-(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];
    }
}

另外,我在 viewDidAppear 方法中调用它,因为当我在 viewDidLoad 中调用它时,没有一个按钮和标签给我一个有效的框架(我猜它们还没有初始化)。

最佳答案

您的 Xcode 屏幕截图显示您的 Storyboard包含约束,因此我们可以判断您的 Storyboard已启用自动布局。如果您想直接在代码中设置 View 框架,则必须关闭自动布局。

看看my answer here如果您需要帮助关闭 Storyboard上的自动布局。

关于objective-c - 以编程方式更改 iPhone 5 的 UIView 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13244803/

相关文章:

ios - 如何在 iOS 上获取照片库中的图像拍摄位置(不是 GPS 坐标)?

ios - UITableViewCell ImageView 填充

objective-c - 自定义 iPhone 应用程序任务栏

ios - 以编程方式定义的不同行为 NSLayoutConstraints 与 'greaterThanOrEqual' 的 Storyboard 约束

ios - ionic ios 应用程序中的 didRangeBeaconsInRegion 事件中的信标数组为空

ios - 在文本属性字典中实例化 UIFont 时出错

objective-c - 使用 Microsoft Exchange Services 2007 的 Cocoa Mac 应用程序

iphone - iOS分段控制问题

objective-c - -[UITableView _endCellAnimationsWithContext :] 中的断言失败

iphone - 如何在 map 注释 View 中找出引脚 ID?