UIButton: почему мое UIControlStateSelected смещение изображения? - PullRequest
2 голосов
/ 31 марта 2012

У меня есть пользовательская панель вкладок и контроллер, взятые из этой статьи iDev Recipes . У меня 2 вопроса:

  1. Мое изображение UIControlStateSelected слишком высоко по вертикали.
  2. Текст кнопки опускается в правый нижний угол выбранного элемента.

Я установил границу зеленого слоя для обозначения границ кнопки:

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);      
}
...