objective-c - 如果 drawRect 被覆盖,iOS subview 显示为黑色矩形

标签 objective-c ios uiview

我有一个加载自定义 UIView 的 Storyboard。还向 Storyboard 中的 View 添加了一个 subview 。它工作正常,直到我覆盖了 subview 的 drawRect 方法,然后我才看到一个黑色矩形而不是 subview 。这是代码:

#import <UIKit/UIKit.h>
#import "MySubview.h"

@interface MyView : UIView

@end

#import "MyView.h"

@implementation MyView

- (void) awakeFromNib
{
    CGRect frame = [self frame];
    MySubview* sv = [[MySubview alloc] initWithFrame:frame];
    [self addSubview:sv];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

@end

#import <UIKit/UIKit.h>

@interface MySubview : UIView

@property (retain, nonatomic) NSString* text;
@property (retain, nonatomic) UILabel* label;

@end

#import "MySubview.h"

@implementation MySubview

@synthesize text, label;


- (void)attachLabel
{
    text = @"Hello";
    label = [[UILabel alloc] init];
    [label setText:text];
    [label setFont:[UIFont fontWithName:@"Futura" size:18]];
    [label setBackgroundColor:[UIColor clearColor]];

    [label sizeToFit];

    CGRect labelFrame = label.frame;
    labelFrame.origin.x = (self.frame.size.width  - labelFrame.size.width) / 2;
    labelFrame.origin.y = (self.frame.size.height - labelFrame.size.height) / 2;
    label.frame = labelFrame;

    [self addSubview:label];
}

- (void)awakeFromNib
{
    [self attachLabel];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self attachLabel];
    }
    return self;
}

// Works if I comment this out!
- (void)drawRect:(CGRect)rect
{
}

@end

更新 - 添加了以下绘图代码:

- (void)drawRectWithRoundBorders:(CGRect)rect
{
    [super drawRect:rect];

    // Parameters used for drawing.
    const CGFloat lineWidth = 5;
    const CGFloat shadowOffset = 3;
    const CGFloat shadowBlur = 4;
    const CGFloat spaceToBB = 10;   // Space to the bounding box of this view.
    const CGFloat cornerRadii = 5;
    const CGFloat lineColor[4] = { 0, 0, 0, 1 };

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    CGContextSetLineWidth(ctx, lineWidth);
    CGContextSetStrokeColor(ctx, lineColor);
    CGContextSetShadow(ctx, CGSizeMake(shadowOffset, shadowOffset), shadowBlur);

    CGRect innerRect = rect;

    innerRect.size.width -= 2*spaceToBB;
    innerRect.size.height -= 2*spaceToBB;
    innerRect.origin.x += spaceToBB;
    innerRect.origin.y += spaceToBB;

    UIBezierPath *path = 
    [UIBezierPath bezierPathWithRoundedRect:innerRect 
                          byRoundingCorners:UIRectCornerAllCorners 
                                cornerRadii:CGSizeMake(cornerRadii, cornerRadii)
     ];

    CGContextAddPath(ctx, path.CGPath);
    CGContextStrokePath(ctx);
}


- (void)drawRect:(CGRect)rect
{
    [self drawRectWithRoundBorders:rect];
}

更新

当我先用某种颜色填充 subview 的边界框时,它似乎起作用了。

CGContextRef ctx = UIGraphicsGetCurrentContext();
CGFloat white[] = {1, 1, 1, 0.5};
CGContextSetFillColor(ctx, white);
CGContextAddRect(ctx, rect);
CGContextFillPath(ctx);

最佳答案

只需在 initWithFrame: 方法中添加 [self setOpaque:NO]

关于objective-c - 如果 drawRect 被覆盖,iOS subview 显示为黑色矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8711499/

相关文章:

iphone - 我的应用使用 URLRequest 登录两次

ios - 不等式约束歧义

ios - 使 UIView 填充 UIScrollview

ios - 解析提要,将其保存在本地的最佳方式是什么?

iOS:找出小数位数

ios - 如何通过外部附件框架使用蓝牙 PAN 配置文件

ios - UITableView 行不展开

swift - 从 .xib 文件加载自定义 UIView 时线程 1 EXC_BAD_ACCESS

objective-c - UIView实现中,什么情况下drawRect : being called?

ios/objective-c : Testing for existence of NSUserDefault