我有一个UIToolbar。
如果我像这样创建一个UIBarButton,它将起作用:
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:iOB style:UIBarButtonItemStylePlain target:self action:@selector(myStuff)];
[button setTitle:@"Hi"];
如果我像这样创建UIBarButton,请使它成为彩色:
UIImageView *vUP = [[UIImageView alloc] initWithImage:iUP];
UIBarButtonItem *UPButton = [[UIBarButtonItem alloc] initWithCustomView:vUP];
[UPButton setAction:@selector(anotherStuff)];
[UPButton setTitle:@"Hi";
[UPButton setTarget:self];
[vUP release];
该按钮显示,没有标题,并且不会响应任何触摸...
我错过了什么吗?
最佳答案
UIImage* image3 = [UIImage imageNamed:@"plain_btn-75-30.png"];
CGRect frameimg = CGRectMake(0, 0, image3.size.width, image3.size.height);
UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];
[someButton setBackgroundImage:image3 forState:UIControlStateNormal];
[someButton addTarget:self action:@selector(backButtonPress:)forControlEvents:UIControlEventTouchUpInside];
[someButton setShowsTouchWhenHighlighted:YES];
[someButton setTitle:@"Back" forState:UIControlStateNormal];
someButton.titleLabel.font = [UIFont fontWithName:@"Helvetica" size:13];
[someButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
UPButton =[[UIBarButtonItem alloc] initWithCustomView:someButton];
如果要同时具有图像和标题,则必须将图像设置为带有标题的背景图像。