Search code examples
iosobjective-ciphone-5

Button with different width on iPhone 5


I want to increase the size of a custom button if the user is using an iPhone 5.

This is what I have in my .m file

//.m File
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
    CGSize result = [[UIScreen mainScreen] bounds].size;
    if(result.height == 480)
    {
        int varWidth = 228;
    }
    if(result.height == 568)
    {
        int varWidth = 272;
    }
}

....

[newButton setFrame:CGRectMake(8.0, 40.0, 228, 80.0)];

But I want something like this:

[newButton setFrame:CGRectMake(8.0, 40.0, varWidth, 80.0)];

Solution

  • You are using varWidth out of it's scope.

    int varWidth;
    
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
        CGSize result = [[UIScreen mainScreen] bounds].size;
        if(result.height == 480)
        {
            varWidth = 228;
        }
        if(result.height == 568)
        {
            varWidth = 272;
        }
    }
    
    ....
    
    [newButton setFrame:CGRectMake(8.0, 40.0, varWidth, 80.0)];