UIButton以编程方式基于设备

问题描述

| 我已经以编程方式创建了一个UIButton,它可以插入到仅iPhone或iPad的应用程序中。是否可以针对通用应用程序执行此操作(即...更改按钮的大小和位置以适合设备UI)?
//insert button for inBox///////////////////////////////////////////////////////////

appButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];

        [appButton setBackgroundImage:[UIImage imageNamed:@\"18-envelope.png\"]       forState:UIControlStatenormal];

        appButton.layer.borderColor=[UIColor clearColor].CGColor;
        appButton.backgroundColor = [UIColor whiteColor];

        appButton.frame = CGRectMake(290.0,25.0,24.0,24.0);
        [appButton addTarget:self
                          action:@selector(showAppInBox)
                forControlEvents:UIControlEventTouchDown];

        [self.window addSubview:appButton];


//END/////////////////////////////////



//Call method to show inBox
-(void) showAppInBox
{
[[AppInBoxManager sharedManager] show];
}
//新代码 //用于INBox的BEGIN插入按钮/////////////////////////////////////////// ////////////////
   if ([[[UIDevice currentDevice] model] isEqualToString:@\"iPhone\"])

    {


    appButton.frame = CGRectMake(200,25,24,16);



   }

    else

    {

    appButton.frame = CGRectMake(700,16);


    }

    appButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];

    [appButton setBackgroundImage:[UIImage imageNamed:@\"envelope_white.png\"] forState:UIControlStatenormal];
    appButton.layer.borderColor=[UIColor clearColor].CGColor;
    appButton.backgroundColor = [UIColor clearColor];



    [appButton addTarget:self
                      action:@selector(showAppInBox)
            forControlEvents:UIControlEventTouchDown];  
    

解决方法

您可以简单地进行以下操作:
if ([[[UIDevice currentDevice] model] isEqualToString: @\"iPhone\"]){


      appButton.frame=XXXX;

}else{

      appButton.frame=YYYY;

}
更多信息: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html     ,@Saimonx感谢您将我推向正确的方向。我找到了答案,并使用以下命令完成了该操作:
 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
//put your code here
}

else

{
//put your code here
}