【问题标题】:Why my UIButton doesn't display in my subview? iPad App为什么我的 UIButton 没有显示在我的子视图中? iPad 应用程序
【发布时间】:2013-11-16 20:29:24
【问题描述】:

有人能帮我理解为什么我的 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 没有?

问候

【问题讨论】:

    标签: ios objective-c ipad uiview uibutton


    【解决方案1】:

    不要这样做:

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

    使用

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

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

    【讨论】:

    • 感谢 jrturton 和 Leijonien 的快速解答
    【解决方案2】:

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

    要确定按钮是否“存在”,您可以给按钮设置一个背景色,如下所示:

    selectCustomerButton.backgroundColor = [UIColor redColor];
    

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

    【讨论】:

    • [UIColor redColor]: 布局调试器的朋友
    猜你喜欢
    • 2011-04-07
    • 1970-01-01
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多