如何设置UIToolBar的标题?

Kel*_*vin 18 objective-c ipad

如何设置UIToolBar的标题,使其看起来与UINavigationBar中的标题相同?

我尝试使用普通样式的按钮,它看起来不错,但是当我点击它时它会突出显示...有没有更好的方法在拆分视图的详细视图中设置标题?

sds*_*kes 27

这就是我用来在工具栏上显示标题时按下时不会突出显示的标题:

#define UIColorFromRGB(rgbValue) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

// choose whatever width you need instead of 600
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 600, 23)];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.shadowColor = UIColorFromRGB(0xe5e7eb);
label.shadowOffset = CGSizeMake(0, 1);
label.textColor = UIColorFromRGB(0x717880);
label.text = @"your title";
label.font = [UIFont boldSystemFontOfSize:20.0];
UIBarButtonItem *toolBarTitle = [[UIBarButtonItem alloc] initWithCustomView:label];
[label release];
Run Code Online (Sandbox Code Playgroud)


Ray*_*eck 9

这解决了突出显示问题,禁用问题和触摸问题.

工具栏和titleButton都是在IB中创建的.视图标题由工具栏覆盖.所以把标题放在工具栏中.

self.titleButton.title = @"Order";  // myInterestingTitle
Run Code Online (Sandbox Code Playgroud)

它看起来像这样: 在此输入图像描述

禁用以防止任何突出显示,并阻止它响应触摸.

self.titleButton.enabled=NO;
Run Code Online (Sandbox Code Playgroud)

然后它看起来像这样:在此输入图像描述

它将看起来处于禁用状态,因此将禁用的颜色设置为白色,其隐式alpha = 1.0.这有效地覆盖了"禁用"外观.

[self.titleButton setTitleTextAttributes:
        [NSDictionary dictionaryWithObject:[UIColor whiteColor]
                                    forKey:UITextAttributeTextColor]
                                forState:UIControlStateDisabled ];
Run Code Online (Sandbox Code Playgroud)

这是你得到的: 在此输入图像描述


小智 7

我认为这会更清洁:

UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:frame];

UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"Your Title" 
                                                         style:UIBarButtonItemStylePlain 
                                                        target:nil 
                                                        action:nil];

UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                        target:nil 
                                                                        action:nil];

NSArray *items = [[NSArray alloc] initWithObjects:spacer, item, spacer, nil];

[toolbar setItems:items];
toolbar.userInteractionEnabled = NO;
Run Code Online (Sandbox Code Playgroud)

  • 虽然将工具栏userInteractionEnabled设置为NO确实会阻止标题的突出显示,但它也会阻止与可能存在的任何其他按钮栏项的交互. (6认同)