簡體   English   中英

iOS 6和自動布局自定義按鈕高度錯誤

[英]IOS 6 and Auto Layout custom button height wrong

我創建了一個非常簡單的自定義平面按鈕:

@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]時,它在標簽周圍具有適當的插圖。

我究竟做錯了什么?

您需要執行以下一項或多項操作:

添加高度限制。

覆蓋internalSize返回值。

調整垂直內容的優先級。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM