有人可以帮助我理解为什么我的 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/