我在界面构建器中添加了一个工具栏。我需要以编程方式添加一些按钮。所以我所做的是在viewDidLoad方法中调用此方法。这些项目在执行操作方法时添加,但它们不可见。
- (void) addingNumberOfLettersBar {
UIImage *image1 = [UIImage imageNamed:@"1"];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setImage:image1 forState:UIControlStateNormal];
//UIBarButtonItem *barButtonItem1 = [[UIBarButtonItem alloc] initWithImage:image1 style:UIBarButtonItemStylePlain target:nil action:@selector(changeNumberOfLettersToShow:)];
//UIBarButtonItem *barButtonItem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:@selector(changeNumberOfLettersToShow:)];
UIBarButtonItem *barButtonItem1 = [[UIBarButtonItem alloc] initWithCustomView:button1];
barButtonItem1.tag = 1;
UIImage *image3 = [UIImage imageNamed:@"3"];
UIBarButtonItem *barButtonItem3 = [[UIBarButtonItem alloc] initWithImage:image3 style:UIBarButtonItemStylePlain target:nil action:@selector(changeNumberOfLettersToShow:)];
barButtonItem3.tag = 3;
UIImage *image4 = [UIImage imageNamed:@"4"];
UIBarButtonItem *barButtonItem4 = [[UIBarButtonItem alloc] initWithImage:image4 style:UIBarButtonItemStylePlain target:nil action:@selector(changeNumberOfLettersToShow:)];
barButtonItem4.tag = 4;
UIImage *image5 = [UIImage imageNamed:@"5"];
UIBarButtonItem *barButtonItem5 = [[UIBarButtonItem alloc] initWithImage:image5 style:UIBarButtonItemStylePlain target:nil action:@selector(changeNumberOfLettersToShow:)];
barButtonItem5.tag = 5;
UIImage *image5p = [UIImage imageNamed:@"5+"];
UIBarButtonItem *barButtonItem5p = [[UIBarButtonItem alloc] initWithImage:image5p style:UIBarButtonItemStylePlain target:nil action:@selector(changeNumberOfLettersToShow:)];
barButtonItem5p.tag = 0;
UIBarButtonItem *flexibleItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
NSArray *toolbarItems = [NSArray arrayWithObjects:barButtonItem1, flexibleItem, barButtonItem3, flexibleItem, barButtonItem4, flexibleItem, barButtonItem5, flexibleItem, barButtonItem5p, nil];
self.toolbar.items = toolbarItems;
}
答案 0 :(得分:0)
检查图像大小。它不应该太大。为每个按钮设置色调颜色并检查它是否可见。
您应该在案例中创建barbuttonitem
,例如
UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"img1"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonClick:)];