Search code examples
iphoneobjective-ccocoacocoa-touchnsbutton

How can I calculate the center of an NSButton?


I'm not sure how to find the center of my button:

spinner = [[EKActivityIndicatorView alloc] initWithFrame:CGRectMake([followButton bounds].origin.x/2, [followButton bounds].origin.y/2, 16, 16)];

Dividing the frame.x and frame.y does not do the trick


Solution

  • The correct way is to do:

    CGRectMake(CGRectGetMidX([followButton frame]), CGRectGetMidY([followButton frame]), 16, 16)
    

    Alternatively, the correct formula is:

    CGRect frame;
    CGPoint center;
    frame = [view frame];
    center.x = frame.origin.x + (frame.size.width / 2);
    center.y = frame.origin.y + (frame.size.height / 2);