如果drawRect被覆盖,iOS子视图显示为黑色矩形 [英] iOS subview displayed as a black rectangle if drawRect is overwritten

查看:328
本文介绍了如果drawRect被覆盖,iOS子视图显示为黑色矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个故事板,可以加载自定义UIView.还将一个子视图添加到情节提要中的视图.在我重写子视图的drawRect方法之前,它一直工作良好,然后我看到的是黑色矩形而不是子视图.这是代码:

I have a storyboard which loads loads a custom UIView. Also a sub view is added to the view in the storyboard. It worked fine until I overwrote the drawRect method of the sub view, then I just saw a black rectangle instead of the subview. Here is the code:

#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

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

Update - Added drawing code below:

- (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];
}

更新

当我先用某种颜色填充子视图的边界框时,它似乎起作用.

It seems to work when I fill the bounding box of the sub view with some color first.

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

推荐答案

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

这篇关于如果drawRect被覆盖,iOS子视图显示为黑色矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆