简体   繁体   English

UIButton包含文本上方图像的背景大小

[英]Background size with UIButton containing image above the text

I have an image displaying correctly above the text and the selected background is the correct size thanks to a wee hack (looping over the subviews). 我有一个在文字上方正确显示的图像,并且由于微不足道(在子视图上循环),选定的背景尺寸正确。

The problem is with the coloured background springing back to the frame of the text, and then animating back to the correct size. 问题在于彩色背景会弹回文本框架,然后再动画设置为正确的大小。

How can I prevent this strange background animation? 如何防止这种奇怪的背景动画?

在此处输入图片说明

- (void)layoutSubviews
{
    [super layoutSubviews];

    CGFloat marginTop = 0;
    CGFloat marginBottom = 0;

    CGSize titleSize = self.titleLabel.frame.size;
    self.imageEdgeInsets = UIEdgeInsetsMake(  - (titleSize.height - marginTop),
                                              0.0,
                                              0.0,
                                              - titleSize.width);

    CGSize imageSize = self.imageView.frame.size;
    self.titleEdgeInsets = UIEdgeInsetsMake(  0.0,
                                              - imageSize.width,
                                              - (imageSize.height - marginBottom),
                                              0.0);

    for (UIView *view in self.subviews) {
        if ([view isKindOfClass:[UIImageView class]] && ![view isEqual:self.imageView]) {
            view.frame = self.bounds;
        }
    }
}

I think some animation is committed at somewhere. 我认为某些地方有动画。 As you are using some view-hierarchy hack, then it might be hidden. 当您使用某些视图层次黑客时,它可能被隐藏了。

Try to avoid animation by calling setAnimationsEnabled: method. 尝试通过调用setAnimationsEnabled:方法来避免动画。

   [UIView setAnimationsEnabled:NO];
   for (UIView *view in self.subviews) {
        if ([view isKindOfClass:[UIImageView class]] && ![view isEqual:self.imageView]) {
            view.frame = self.bounds;
        }
    }
   [UIView setAnimationsEnabled:YES];

Beware. 谨防。 This is another hack based on global state, and may cause even worse side effect. 这是基于全局状态的另一种入侵,可能会导致更严重的副作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM