iPhone SDK:iPhone和iPad如何解决UI布局差异? [英] iPhone SDK: How to account for UI layout differences, iPhone vs iPad?

查看:77
本文介绍了iPhone SDK:iPhone和iPad如何解决UI布局差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个通用应用程序(可同时在iPhone和iPad上运行),如果设备之间的UI不同,我该如何解释代码中的UI布局差异?

If I have a universal app (runs on both iPhone & iPad), how do I account for UI layout differences in code if the UI differs between the devices?

如果我使用的是XIB,则可以根据设备简单地加载不同的XIB,并使用相同的代码.正确吗?

If I am using a XIB, I can simply load a different XIB depending on the device and use the same code. Correct?

但是,如果我以编程方式创建UI,应该如何处理呢?是否有比通过代码查看设备类型更好的方法?我知道这样做是可行的,只是看起来不太优雅.

If I am creating my UI programmatically, though, how should this be handled? Is there a better way than if/elsing my way through the code and looking at the device type? I understand it's doable this way, it just doesn't seem very elegant.

if (isiPhone)
{        
    UIView *myCommonView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 150.0, 50.0)];
    myCommonView.backgroundColor = [UIColor colorWithPatternImage:iPhoneSpecificImage];
    // bunch of other conditional code
} 
else
{
    UIView *myCommonView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 250.0, 150.0)];
    myCommonView.backgroundColor = [UIColor colorWithPatternImage:iPadSpecificImage];
    // bunch of other conditional code
}

推荐答案

使用多个if/else语句的更好的替代方案是拥有多个应用程序委托.一种简单的方法是创建一个AppDelegate_iPhone& AppDelegate_iPad并将它们分别(在IB中)绑定到在启动时加载的不同XIB.现在,您可以通过编程方式向每个委托添加不同的项目.

A much better alternative to using loads of if/else statements is to have multiple app delegates. An easy way to do this is to create an AppDelegate_iPhone & AppDelegate_iPad and respectively bind them (in IB) to the different XIBs that get loaded on launch. Now you have a way to programmatically add different items to each delegate.

这也可能导致您想要在委托之间共享UI元素的问题.在这种情况下,您可以创建两个委托都使用的共享视图(在两种情况下都只需导入视图控制器即可).

This also can lead to an issue where you want to share UI elements between the delegates. In this case, you can create shared views that are used by both delegates (just import the view controller in both cases).

代码在结构上应类似于.

The code should structurally look something like this.

这篇关于iPhone SDK:iPhone和iPad如何解决UI布局差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆