为什么我的UIButton不会显示在我的子视图中? iPad应用程序

时间:2013-11-16 20:29:25

标签: 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 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;}

为什么UILabel通过实现addSubview方法正确显示而UIButtons不是?

此致

2 个答案:

答案 0 :(得分:1)

不要这样做:

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

使用

[selectCustomerButton setImage:[UIImage imageNamed:@"Farmer.png"] forState:UIControlStateNormal];

按钮根据其状态设置自己的图像视图图像,您无法自行访问和设置图像视图的图像。

答案 1 :(得分:0)

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

要确定按钮是否为“那里”,您可以为按钮提供如下背景颜色:

selectCustomerButton.backgroundColor = [UIColor redColor];

如果您看到一个红色框,则表示图像未正确加载或者甚至找不到。