带背景图片的按钮 - iPhone 5调整大小问题

时间:2012-11-16 01:41:16

标签: iphone xcode uibutton background-image autoresize

我一直在这里寻找谷歌,但我仍然有这个奇怪的问题,我无法想象。我在下面有一个应用程序图片,其中4个UIButton已应用背景图像: enter image description here

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

enter image description here

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

我真的希望这不是重复,但我完全被难过了。如果我沿着UIView在其他位置改变UIButtons,那么另一个将调整大小,或者会出现奇怪的间隙间距....我不明白。谢谢你的帮助!

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

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

2 个答案:

答案 0 :(得分:2)

您可以使用的功能之一称为AutoLayout。它配备了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];

希望它清除一点。

答案 1 :(得分:1)

首先应禁用AutoLayout此选项位于“显示文件检查器”下

相关问题