Scaling issue on different devices

static cocos2d::Size designResolutionSize = cocos2d::Size(320, 480);
#define kButtonMargin 5
#define KNumberButtonPerRow 3

// The size of the button is 100x50
Size buttonSize = Size((this->getContentSize().width - kButtonMargin * 4) / KNumberButtonPerRow, 50.0);

on iPhone 4s it showing as it should while on other devies (iPhone 5, iPhone 6, iPhone 6 Plus, Android Nexus) the buttons are too large as you can see on the screenshot.

For buttons I’m using Scale9Sprite where the size of the sprite is buttonSize.

How can I fix this?

I’ve spent a few hours experimenting different things and searching for help online.
10 minutes after posting this question here I’ve discovered what was the problem.

I was retrieving the size of the scene using the getContentSize() which is wrong.
I replaced it with the following code, and it worked:

auto visibleSize = Director::getInstance()->getVisibleSize();
Size buttonSize = Size((visibleSize.width - kButtonMargin * 4) / KNumberButtonPerRow, 50.0);