Search code examples
ios4xcode4uibuttonipad

UIButton Programmatically based on device


I have created a UIButton programmatically that is inserted on either a iPhone or iPad only application. Is it possible to do it for a universal app (i.e....change the size of the button and location to fit the device 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];
}

//new code //BEGIN insert button for 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];  

Solution

  • You can simply make the following:

    if ([[[UIDevice currentDevice] model] isEqualToString: @"iPhone"]){
    
    
          appButton.frame=XXXX;
    
    }else{
    
          appButton.frame=YYYY;
    
    }
    

    More Information in:

    http://developer.apple.com/library/ios/#documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html