UILabel未显示在自定义UIView屏幕中

时间:2015-12-10 12:21:40

标签: ios objective-c iphone uiview uilabel

以下是我实现自定义UiView的方法

在myView.h文件中

@interface myView : UIView
{
    NSString *message;
}
...
@property (nonatomic, retain) UILabel *messageLabel;
...
@end

在myView.m文件中

此函数将实例化myView并向其添加消息标签

+ (id) initWithText:(NSString *) text
{
    screenBounds = [[UIScreen mainScreen] bounds];
    CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];

    self = [super initWithFrame:CGRectMake(0, statusBarFrame.size.height, screenBounds.size.width, 40)];

    if (self)
    {
        [self setBackgroundColor:[UIColor redColor]];

        message = [text copy];
        _messageLabel = [[UILabel alloc]initWithFrame:[self frame]];
        [_messageLabel setText:message];
        [_messageLabel setAdjustsFontSizeToFitWidth:YES];
        [_messageLabel setTextAlignment:NSTextAlignmentJustified];
        [_messageLabel setTextColor:[ UIColor blackColor]];
        [self addSubview:_messageLabel];
    }   
   return self;
}

稍后我将myView作为子类添加到我的屏幕中的可见视图中。当我运行应用程序时,我可以看到红色的myView,但消息标签没有显示在其中。

4 个答案:

答案 0 :(得分:2)

使用self.frame初始化UILabel时,必须考虑其父视图中的值。

也许你的第二个参数:y = statusBarFrame.size.height很高,这就是为什么你的标签不在你看来?

尝试使用CGRectMake(0,0,self.frame.width,self.frame.height)初始化您的标签

答案 1 :(得分:1)

检查您在标签中设置的文字是否为空白

_messageLabel = [[UILabel alloc]initWithFrame:self.frame];

答案 2 :(得分:0)

我试过这段代码并且它有效。当然你没有在其他地方为你的UILabel设置其他参数?这些是与您的代码的不同之处:

  • 我更换了你的' +' by' - '对于该方法。
  • 我改变了uilabel框架进行初始化。
  • 为标签添加了背景颜色(仅用于检查)。

    vagrant@debian-jessie:~$ systemctl status nginx.service                                                                          
    nginx.service - A high performance web server and a reverse proxy server
       Loaded: loaded (/lib/systemd/system/nginx.service; enabled)
       Active: failed (Result: exit-code) since Thu 2015-12-10 14:34:59 GMT; 4min 53s ago
      Process: 6023 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS)
      Process: 6043 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=1/FAILURE)
     Main PID: 3899 (code=exited, status=0/SUCCESS)
    vagrant@debian-jessie:~$ journalctl -xn
    No journal files were found
    

答案 3 :(得分:0)

之前:我曾经打电话给backgroundOP.m&来自initWithText()文件的show()我在那里进行后台操作。这些函数总是在不同的线程中执行。

现在:我将电话转移至backgroundOP.m&amp;委托方法中的UIView方法,并将委托添加到呈现UIViewController,即在我希望我的视图出现的viewController.m文件中。现在我从另一个文件(即UILabel)调用此委托,问题就解决了。现在屏幕上都可以看到UIWindow *window = [[UIApplication sharedApplication] keyWindow]; dispatch_async(dispatch_get_main_queue(), ^{ [window addSubview:self]; // self represents myView object }); <ItemTemplate> <div class="divItemTotal"> <div> <asp:Label runat="server" CssClass="totalItem" ID="lblItemTotalPrice" Text='<%#: String.Format("{0:N2}", ((Convert.ToDouble(Item.Quantity)) * Convert.ToDouble(Item.UnitPrice)))%>'></asp:Label> <asp:Label runat="server" ID="lblCurrency2" Text=" €"></asp:Label></div> <asp:LinkButton CommandArgument='<%# Eval("Item.Id") %>' CommandName="Delete" CssClass="removeItem imgRemoveItem" runat="server" ID="DeleteRow"></asp:LinkButton> </div>

但我不明白为什么从后台线程函数添加UIView只显示UIView而不显示其内容/子视图。

在我的show()函数中,我使用此调度队列将myView添加到呈现关键窗口。如下面的代码

protected void CartList_RowDataBound(object sender, GridViewRowEventArgs e)
{ 
LinkButton l = (LinkButton)e.Row.FindControl("DeleteRow"); 
l.Attributes.Add("onclick", "javascript:return " +
"confirm('Are you sure you want to delete this record " +
DataBinder.Eval(e.Row.DataItem, "Item.Id") + "')");
}



protected void GridView1_RowCommand(object sender, 
                     GridViewCommandEventArgs e)
{
 if (e.CommandName == "Delete")
 {
   // get the categoryID of the clicked row
  int categoryID = Convert.ToInt32(e.CommandArgument);
  // Delete the record 
  DeleteRecordByID(categoryID);
// Implement this on your own :) 
}
}

如果有人知道这种行为的原因,请赐教。