簡體   English   中英

適用於iPhone和iPad的單一NIB

[英]Single NIB for iPhone and iPad

我在這里和谷歌看到了類似的問題,但沒有人回答我的實際問題。 我想為iPhone和iPad配備一個NIB。 NIB本身應該只有一個空視圖。 應該進入視圖的所有內容都是以編程方式完成的。 我檢查設備是否是iPhone / iPad,然后計算並設置布局。 一點點工作,但對於所需的任務,這對我來說是最好的解決方案。

但是當通過“新文件... - >用戶界面 - >視圖”添加新視圖時,我被要求提供設備系列。 選擇當然是iPhone或iPad。 但我希望視圖可以在兩種設備上工作,一種通用的。

我嘗試了iPhone視圖,它實際上適用於iPhone和iPad兩種設備。 所以似乎一切都很好。 我的問題更通用,詢問是否可以重新使用iPhone的iPhone視圖。 我應該換一種方式嗎? 更好的解決方案?

請理解我真的想以單一視圖和編程方式工作,因此請為每個設備使用單獨視圖並使用IB的解決方案請保留!

謝謝

如果以編程方式創建所有內容,則根本不需要使用nib來創建空視圖。

你的UIViewController子類應該是這樣的

@implementation MyViewController
-(void)loadView
{
    //Do not call [super loadView] in your implementation. 
    //The super implementation loads the nib based on the nibName and nibBundle properties. 
    UIView * view = [[UIView alloc] init];
    //Add subviews, etc

    //You must assign a UIView object to the view property before loadView completes
    self.view = view;
}
@end

您可以使用以下任何格式實例化視圖控制器:

MyViewController * controller = [[MyViewController alloc] initWithNibName:nil bundle:nil];
MyViewController * controller = [[MyViewController alloc] init];
MyViewController * controller = [MyViewController new];

首先,你可以使用AutoResize> LINK來做這件事

如果它變得復雜,那么你可以使用以下功能並以編程方式自己完成。

-(int)setScreenOf
{

    int size=0;
    if((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad))
    {
        //ipad 
         size=3;

    }
    else
    {

        CGRect screenBounds = [[UIScreen mainScreen] bounds];
        if (screenBounds.size.height ==568)
        {
             size=2;
            // code for 4-inch screen           
        }
        else
        {
            size=1;
            // code for 3.5-inch screen           
        }  
    }
    return  size;    
}

或者,如果您想要進行設備版本特定代碼,請嘗試此操作。

-(NSString *)deviceVersion
{
    size_t size;
    sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    char *answer = (char*)malloc(size);
    sysctlbyname("hw.machine", answer, &size, NULL, 0);
    NSString *platform = [NSString stringWithCString:answer encoding: NSUTF8StringEncoding];
    free(answer);
    NSLog(@"Platform: %@", platform);
    return platform;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM