1

好吧,我已经做了一百万次这种事情,但这个特殊情况让我无法理解,我敢肯定它只是我想念的一些愚蠢的事情......

但我试图在 mapView 上显示一个 UIView,以给人一种 mapview 被“禁用”的印象。

我创建了一个 UIView 子类并添加了一个标签。像这样...

。H

  #import <UIKit/UIKit.h>

  @interface DisabledOverlayView : UIView

  @property (nonatomic, strong) UILabel *disabledOverlayViewText;

  @end

没什么复杂的...

在我的 .m 中,我设置了文本并将其添加到我的 UIView 中。

@synthesize disabledOverlayViewText = _disabledOverlayViewText;


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

    self.backgroundColor = [UIColor blackColor];
    self.alpha = 1;

    _disabledOverlayViewText.textColor = [UIColor whiteColor];
    _disabledOverlayViewText.frame = CGRectMake(0, 0, 200, 200);
    // [_disabledOverlayViewText setCenter:self.center];
    _disabledOverlayViewText.text = @"testing";
    [self addSubview:_disabledOverlayViewText];
}
return self;

}

然后我希望使用这个覆盖的类我称之为......

    DisabledOverlayView *disabledView = [[DisabledOverlayView alloc] initWithFrame:CGRectMake(12, 12, _mapView.frame.size.width, _mapView.frame.size.height)];
    disabledView.disabledOverlayViewText.text = @"No Location";
    [self.view addSubview:disabledView];

这些都不是太复杂..但标签没有出现。覆盖看起来很好,但没有标签文本。我弄乱了标签的框架,认为它可能不在可见视图中,但没有骰子。我无法弄清楚发生了什么。

4

2 回答 2

1

initWithFrame用下面的代码替换你的方法。我添加了 alloc init 行并更改了label. 试试看它是否有效:)

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

        self.backgroundColor = [UIColor blackColor];
        self.alpha = 1;

       _disabledOverlayViewText = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
       _disabledOverlayViewText.textColor = [UIColor whiteColor];
       _disabledOverlayViewText.backgroundColor = [UIColor blackColor];
        // [_disabledOverlayViewText setCenter:self.center];
       _disabledOverlayViewText.text = @"testing";
       [self.view addSubview:_disabledOverlayViewText];

}
于 2013-02-18T17:09:28.570 回答
0

似乎标签没有分配,它是零。

试试这个

  • (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; 如果(自我){

    self.backgroundColor = [UIColor blackColor]; self.alpha = 1;

    self.disabledOverlayViewText = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];

    _disabledOverlayViewText.textColor = [UIColor whiteColor];

    // [_disabledOverlayViewText setCenter:self.center];

    _disabledOverlayViewText.text = @"测试";

    [self addSubview:_disabledOverlayViewText]; }

于 2013-02-18T17:13:23.633 回答