How to set the title of UIToolBar?

后端 未结 8 1127
独厮守ぢ
独厮守ぢ 2021-02-01 07:09

How can I set the title of UIToolBar such that it looks the same as the title in UINavigationBar?

I tried to use a button with plain style, it looks ok, but it will be

相关标签:
8条回答
  • 2021-02-01 07:38

    Swift 5 version of Rayfleck's great answer:

    let titleButton = UIBarButtonItem(title: "My Title", style: .plain, target: nil, action: nil)
    titleButton.isEnabled = false
    titleButton.setTitleTextAttributes([.foregroundColor : UIColor.black], for: .disabled)
    

    Add this button to the toolbar as normal.

    0 讨论(0)
  • 2021-02-01 07:40

    This solves the highlighting problem, disabled problem, and touch problem.

    Toolbar and titleButton were both created in IB. The view title is covered by the toolbar. So put the title in a toolbar.

    self.titleButton.title = @"Order";  // myInterestingTitle
    

    It looks like this: enter image description here

    Disable to prevent any highlighting, and to stop it from responding to touches.

    self.titleButton.enabled=NO;
    

    Then it looks like this:enter image description here

    It will look disabled, so set the color for disabled to white, which has an implicit alpha=1.0. This effectively overrides the 'disabled' look.

    [self.titleButton setTitleTextAttributes:
            [NSDictionary dictionaryWithObject:[UIColor whiteColor]
                                        forKey:UITextAttributeTextColor]
                                    forState:UIControlStateDisabled ];
    

    Here's what you get: enter image description here

    0 讨论(0)
提交回复
热议问题