IOS 6和自动布局自定义按钮高度错误

时间:2013-08-02 23:01:46

标签: ios uibutton autolayout

我创建了一个非常简单的自定义平面按钮:

@implementation HAFlatButton

+ (id)buttonWithColor:(UIColor *)aColor
{
    id button = [super buttonWithType:UIButtonTypeCustom];

    [button setFlatColor:aColor];

    CGRect frame = [button frame];
    frame.size.height = 200;
    [button setFrame:frame];

    return button;
}

+ (id)defaultButton
{
    return [HAFlatButton buttonWithColor:[HAColors buttonColor]];
}

- (void)setFlatColor:(UIColor *)flatColor
{
    _flatColor = flatColor;
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = UIGraphicsGetCurrentContext();

    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:4.0];
    CGContextSaveGState(context);
    [path addClip];

    [_flatColor setFill];
    CGContextFillRect(context, rect);

    CGColorSpaceRelease(colorSpace);
}

@end

当我使用[HAFlatButton defaultButton]向我的自动布局添加一个按钮时,它只比按钮文本高一点,但是当我在我的布局中添加[UIButton buttonWithType:UIButtonTypeRoundedRect]时,它在标签周围有适当的插图。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

您需要执行以下一项或多项操作:

添加高度约束。

覆盖intrinsicSize返回值。

调整垂直内容拥抱优先级。

相关问题