UIButton:为什么我的UIControlStateSelected图像偏移?

时间:2012-03-31 14:00:31

标签: iphone objective-c uibutton uicontrol

我有一个自定义标签栏和控制器取自iDev Recipes article。我有两个问题:

  1. 我的UIControlStateSelected图像垂直太高。
  2. 按钮的文字会显示在所选项目的右下角。
  3. 我设置了一个绿色图层边框来表示按钮边框:

    enter image description here

    我已经确定偏移是由于我在下面使用的代码将文本和图像置于按钮中心。为什么会导致上面的#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);      
    }
    

0 个答案:

没有答案