ios - 为什么我的 UIButton 没有显示在我的 subview 中? iPad应用程式

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

为什么 UILabels 通过实现 addSubview 方法可以正确显示,而 UIButtons 却不能?

问候

最佳答案

不要这样做:

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

使用

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

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

关于ios - 为什么我的 UIButton 没有显示在我的 subview 中? iPad应用程式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20023502/

相关文章:

ios - 在 Swift 中使用 RSSBlockParser 成功 block

ios - 如何从另一个 uitableviewcell 中的 tableview 导航?

objective-c - c风格的指针和id风格的对象有什么区别?

iphone - 如何在iphone中设置html +css add

iphone - iAds : How can I prevent my AdBannerView delegate from getting called after the view has been released?

ios - 如何将图像数组从 tableView 传递到另一个 DetailView

ios - 如何使用 Twilio Swift SDK 取消注册 VOIP 推送通知

ios - 调整大小并在横向放置旋转 View : updating constraints or translating/changing frames?

objective-c - 在 UITabBarController 中监听 View Controller 的编程更改

iphone - 默认情况下,UITableView编辑模式