我有一个自定义标签栏和控制器取自iDev Recipes article。我有两个问题:
我设置了一个绿色图层边框来表示按钮边框:
我已经确定偏移是由于我在下面使用的代码将文本和图像置于按钮中心。为什么会导致上面的#1和#2问题?
-(void) centerButtonImageAndText: (UIButton*) button {
NSLog(@"CustomTabBar centerButtonImageAndText");
// the space between the image and text
CGFloat spacing = 0.0;
// mark: this offsets the verticalOffset above for placing the button due to the controller's selectedItemBackgroundImage: method
CGFloat textAndImageOffset = TEXT_AND_IMAGE_OFFSET;
// get the size of the elements here for readability
CGSize imageSize = button.imageView.frame.size;
CGSize titleSize = button.titleLabel.frame.size;
// lower the text and push it left to center it
button.titleEdgeInsets = UIEdgeInsetsMake(
0.0 + textAndImageOffset, - imageSize.width, - (imageSize.height + spacing), 0.0);
// the text width might have changed (in case it was shortened before due to
// lack of space and isn't anymore now), so we get the frame size again
titleSize = button.titleLabel.frame.size;
// raise the image and push it right to center it
button.imageEdgeInsets = UIEdgeInsetsMake(
- (titleSize.height + spacing) + textAndImageOffset, 0.0, 0.0, - titleSize.width);
}