【问题标题】:Button with background image - iPhone 5 resize issue带有背景图像的按钮 - iPhone 5 调整大小问题
【发布时间】:2012-11-04 17:46:56
【问题描述】:

我一直在这里和谷歌上到处寻找,但我仍然有这个我无法弄清楚的奇怪问题。我在下面有一个应用程序图片,其中有 4 个 UIButtons 应用了背景图像:

当我在 iPhone 5 上调整/运行时,顶部按钮以一种非常奇怪的方式调整大小:

如何让所有 UIButtons 以与 iPhone 5 相同的方式调整大小?可能吗?我需要使用其他方法吗?

我真的希望这不是重复的,但我完全被难住了。如果我在 UIView 的其他位置更改 UIButtons,则会调整不同的 UIButtons 的大小,或者会出现奇怪的间隙间距......我不明白。感谢您的帮助!

编辑:我在 Xcode 4.5 上使用 iOS 6。我在 iPhone 4S 和 iPhone 5 上进行测试。我检查了 AutoLayout。如果我使用 Pin(刚刚找到),我可以让 UIButton 保持其高度。

澄清我的问题。我希望 UIButtons 调整大小,使它们占据屏幕的相同百分比......我希望比例相同,而不是简单地添加一堆空白空间。我希望它更清楚

【问题讨论】:

    标签: iphone xcode uibutton background-image autoresize


    【解决方案1】:

    您可以使用的功能之一称为自动布局。它与 Xcode 一起提供,以便更轻松地调整 4 英寸屏幕的内容。你可以在这里找到一个很好的例子:http://www.raywenderlich.com/20881/beginning-auto-layout-part-1-of-2

    但据我所知,AutoLayout 仅适用于 iOS 6,这使得它“还没有那么有用”。我一直在使用一个名为UIDevice 的类,其来源可以在这里找到:https://github.com/erica/uidevice-extension 并检查平台类型是否为 iPhone 5。我将它设置在一个在视图加载时设置 GUI 的方法中。我的实现示例:

    UIImage *backgroundImage;
    
    if ([[UIDevice currentDevice] platformType] == UIDevice5iPhone)
    {
        //I have a 4 inch screen present
        myButton.frame = CGRectMake(x, y, w, h); //Set the frame as you would like it to look on the iPhone 5
    
        backgroundImage = [[UIImage alloc] initWithCGImage:[UIImage imageNamed:@"main_background-568h@2x"].CGImage scale:2.0 orientation:UIImageOrientationUp];
    }
    else
    {
        //I have a 3.5 inch screen present
        myButton.frame = CGRectMake(x, y, w, h); //Set the frame as you would like it to look on the iPhone 3, 3gs, 4, 4s...
    
        backgroundImage = [[UIImage imageNamed:@"main_background"] retain];
    }
    self.view.backgroundColor = [UIColor colorWithPatternImage:backgroundImage];
    [backgroundImage release];
    

    希望它清除一点。

    【讨论】:

    • 我使用的是 iOS 6 并且我检查了 AutoLayout。这是 Xcode 4.5 中的一个问题,并且在 iPhone 4S 和 iPhone 5 上都进行了测试。感谢您的回答,无论如何我都会调查一下。
    • 谢谢,我没有有效地使用 AutoLayout,当我使用 Pin 时,我让 UIButton 保持相同的高度。但是,我想保持相同的比例。我编辑了上面的问题。谢谢!
    • 没问题,如果解决了你的问题记得采纳答案。
    【解决方案2】:

    你应该首先禁用使用 AutoLayout 这个选项在“显示文件检查器”下

    【讨论】:

    • 为什么不应该使用 AutoLayout?
    • 自动布局主要是制造问题,没有使用对不起我不知道自动布局的目的
    • 如果你勾选了AutoLayout,那么应用在低版本iOS上将不兼容并且会崩溃。
    猜你喜欢
    • 1970-01-01
    • 2017-01-04
    • 2017-02-15
    • 1970-01-01
    • 2013-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    相关资源
    最近更新 更多