UIButton以编程方式基于设备

时间:2011-05-26 19:30:21

标签: ios4 xcode4 uibutton ipad

我已经以编程方式创建了一个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];
}

//新代码 // BEGIN INBOX的插入按钮/////////////////////////////////////////// ////////////////

   if ([[[UIDevice currentDevice] model] isEqualToString:@"iPhone"])

    {


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



   }

    else

    {

    appButton.frame = CGRectMake(700, 25, 24, 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];  

2 个答案:

答案 0 :(得分:0)

您可以简单地进行以下操作:

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

答案 1 :(得分:0)

@Saimonx谢谢你让我朝着正确的方向前进。我找到了答案,并使用以下方法完成:

 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
//put your code here
}

else

{
//put your code here
}