Ios 为什么我的UIButton没有';是否在我的子视图中显示?iPad应用程序

Ios 为什么我的UIButton没有';是否在我的子视图中显示?iPad应用程序,ios,objective-c,ipad,uiview,uibutton,Ios,Objective C,Ipad,Uiview,Uibutton,有人能帮我理解为什么我的UIButton没有显示在我的自定义UIView中吗? 在下面的代码中,我发布了initWithFrame方法: - (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { self.backgroundColor =[UIColor whiteColor]; UIToolbar *toolbar = [[UIToolbar a

有人能帮我理解为什么我的UIButton没有显示在我的自定义UIView中吗? 在下面的代码中,我发布了initWithFrame方法:

- (id)initWithFrame:(CGRect)frame{

     self = [super initWithFrame:frame];

    if (self) {
    self.backgroundColor =[UIColor whiteColor];

    UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0,frame.size.width, 50)];
    toolbar.backgroundColor = [UIColor grayColor];

    UIBarButtonItem *cancelBarButton = [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(dismissNewInvoiceView:)];
    UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *saveBarButton = [[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStylePlain target:self action:@selector(saveNewInvoice:)];
    [saveBarButton setEnabled:NO];
    NSArray *arrayOfItems = [[NSArray alloc]initWithObjects:cancelBarButton,flexibleSpace, saveBarButton, nil];
    [toolbar setItems:arrayOfItems];

    UILabel *headerViewLabel = [[UILabel alloc]initWithFrame:CGRectMake(335, 15, 200, 20)];
    headerViewLabel.text = @"New Invoice";
    [toolbar addSubview:headerViewLabel];

    [self addSubview:toolbar];

    UIButton *selectCustomerButton = [[UIButton alloc]initWithFrame:CGRectMake(25, 200, 60, 60)];
    selectCustomerButton.imageView.image = [UIImage imageNamed:@"Farmer.png"];

    [self addSubview:selectCustomerButton];
    [self bringSubviewToFront:selectCustomerButton];

    UILabel *invoiceNumberLabel = [[UILabel alloc]initWithFrame:CGRectMake(25, 55, 150, 25)];
    [invoiceNumberLabel setFont:[UIFont fontWithName:@"Verdana" size:14]];
    invoiceNumberLabel.text = @"Invoice number:";
    [self addSubview:invoiceNumberLabel];

    UITextField *invoiceNumberField = [[UITextField alloc]initWithFrame:CGRectMake(140, 53, 150, 30)];
    [invoiceNumberField setFont:[UIFont fontWithName:@"Verdana" size:25]];
    invoiceNumberField.placeholder = @"25/13";
    [self addSubview:invoiceNumberField];

    UILabel *dateOfCreationLabel = [[UILabel alloc]initWithFrame:CGRectMake(500, 55, 150, 25)];
    [dateOfCreationLabel setFont:[UIFont fontWithName:@"Verdana" size:14]];
    dateOfCreationLabel.text = @"Date of creation:";
    [self addSubview:dateOfCreationLabel];







}
return self;}
为什么UILabels通过ImplementalAddSubView方法正确显示,而UIButtons则没有

关于

不要这样做:

selectCustomerButton.imageView.image = [UIImage imageNamed:@"Farmer.png"]
使用


该按钮根据其状态设置其自己的图像视图图像,您不能自己访问和设置图像视图的图像

如果图像未正确加载/显示,按钮将不可见。正如jrturton已经说过的,您应该使用另一种方法来设置映像

要确定按钮是否在“那里”,您可以为按钮设置如下背景色:

selectCustomerButton.backgroundColor = [UIColor redColor];

如果您看到一个红色框,您就知道图像没有正确加载,甚至可能找不到。

[UIColor redColor]
:布局调试器的朋友感谢jrturton和Leijonien的快速回答
selectCustomerButton.backgroundColor = [UIColor redColor];