CCScale9Sprite *backgroundButton = CCScale9Sprite::create(btnImg, imgRect, imgRectInsets); CCScale9Sprite *backgroundHighlightedButton = CCScale9Sprite::create(hightLightBtnImg, imgRect, imgRectInsets); m_button = CCControlButton::create(backgroundButton); m_button->setZoomOnTouchDown(false); //设置不现实按钮效果 m_button->setBackgroundSpriteForState(backgroundHighlightedButton, CCControlStateHighlighted); m_button->setPreferredSize(CCSizeMake(size.height, size.height)); //m_button->setContentSize(CCSizeMake(size.height, size.height)); m_button->setPosition(m_label->getContentSize().width + m_button->getContentSize().width/2, m_button->getContentSize().height/2); addChild(m_button, 1);
需要注意的是注释的
m_button->setContentSize(CCSizeMake(size.height, size.height));
这句,调整CCControlButton的大小不能用setContentSize, 只能setPreferredSize函数, 否则会导致显示不正确.