我正在使用一个UISegmentedControl内部UIToolBar按钮.我不能正常使用UIButtonBarItem,因为我需要设置tintColor并支持iOS 4.3.所以我使用以下代码:
UISegmentedControl* searchButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"", nil]];
[searchButton setImage:[UIImage imageNamed:@"search_picto"] forSegmentAtIndex:0];
searchButton.momentary = YES;
searchButton.segmentedControlStyle = UISegmentedControlStyleBar;
searchButton.tintColor = [UIColor colorWithRed:0.27 green:0.60 blue:0.20 alpha:1.00];
[searchButton addTarget:self action:@selector(actionSearch:) forControlEvents:UIControlEventValueChanged];
Run Code Online (Sandbox Code Playgroud)
这很好用,但有没有办法在我的图像旁边有文字/标题?方法setTitle:删除图像.必须有另一种方式......
谢谢你的帮助.
-
编辑:我决定直接将文字添加到UIImageView这样:
NSString* label = @"My Label";
UIImage* image = [self addText:label toImage:[UIImage imageNamed:@"icon"]];
[_segmentedControl insertSegmentWithImage:image atIndex:0 animated:NO];
[_segmentedControl setWidth:image.size.width + 10]; // add some margin on the right
- (UIImage *)addText:(NSString *)text toImage:(UIImage *)image {
UIFont* …Run Code Online (Sandbox Code Playgroud)