How can we put two line in UIBarButtonItem in Navigation Bar

前端 未结 4 1929
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-16 21:19

\"Now Playing\" is in One line in UIBarButtonItem. I have to put it in two lines, like \"Now\" is ay top and \"Playing\" is at bottom.I have written the following line of co

4条回答
  •  忘掉有多难
    2020-12-16 21:35

    Yes you can. It is fairly simple to do. Create a multiline button and use that. The "trick" is to set the titleLabel numberOfLines property so that it likes multilines.

    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.titleLabel.numberOfLines = 0;
    [button setTitle:NSLocalizedString(@"Now\nPlaying", nil) forState:UIControlStateNormal];
    [button sizeToFit];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    

    When you specify a button type of custom it expects you to configure everything... selected state, etc. which is not shown here to keep the answer focused to the problem at hand.

提交回复
热议问题