3

我有一个使用 IB 创建的应用程序,带有一个标签栏和几个标签,其中一个标签视图的背景图像无法自动调整大小(拉伸)。该视图有十几个隐藏在背景后面的按钮,并且按钮的位置必须根据背景进行更改。

我正在考虑复制 XIB 文件并编辑 XIB 以选择 568 像素背景图像并相应地重新定位页面下方的按钮。然后在运行时我想添加代码以在 iPhone 5 上选择 568 像素(iPhone x/5 选择不是问题)”。

最后一件事,我想使用相同的视图控制器和连接(如果可能的话),因为所有代码都是通用的。有可能这样做吗?如何创建 XIB 并使其在选项卡选择时可见。

4

1 回答 1

4

You can check the height of the screen, and programmatically choose which xib you want to use:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    MyFirstViewController *vc1 = nil;
    MySecondViewController *vc2 = nil;

    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        CGSize screenSize = [[UIScreen mainScreen] bounds].size;
        if(screenSize.height == 568) {
            vc1 = [[MyFirstViewController alloc] initWithNibName:@"LargeFirstViewController" bundle:nil];
            vc2 = [[MySecondViewController alloc] initWithNibName:@"LargeSecondViewController" bundle:nil];
        }
        if(screenSize.height == 480) {
            vc1 = [[MyFirstViewController alloc] initWithNibName:@"SmallFirstViewController" bundle:nil];
            vc2 = [[MySecondViewController alloc] initWithNibName:@"SmallSecondViewController" bundle:nil];
        }
    }
    else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        // ... Add iPad code here if relevant.
    }

    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = @[vc1, vc2];
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}

To change the tab bar icon image programmatically edit the following in your view controller (replace "YOUR-IMAGE" with the actual name... do not put the extension (such as .png)):

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tabBarItem.image = [UIImage imageNamed:@"YOUR-IMAGE"];
    }
    return self;
}

When you create a new xib file, don't forget to select the "File Owner" (under "Placeholders") and set the "Custom Class" to the actual view controller class in the Attributes Inspector. Also, while the "File Owner" is selected, go to the "Connections Inspector" and drag the "view" outlet to the top level view of your xib.

于 2012-10-30T19:33:02.897 回答