How can we put two line in UIBarButtonItem in Navigation Bar

前端 未结 4 1919
佛祖请我去吃肉
佛祖请我去吃肉 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.

    0 讨论(0)
  • 2020-12-16 21:39

    You can host a UIButton as customView inside your bar button (either set the bar button item customView or you can drag one directly on top of your UIBarButtonItem), hook it up as an outlet, then do;

    -(void) viewDidLoad
    {
      //...
      self.customButton.titleLabel.numberOfLines = 2;
      self.customButton.suggestionsButton.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
      self.customButton.suggestionsButton.titleLabel.textAlignment = UITextAlignmentCenter;
    }
    

    which in my case becomes

    enter image description here

    0 讨论(0)
  • 2020-12-16 21:44

    enter image description here enter image description here

    I create 2 PNG images by myself, and it looks good.

    UIImage *img = [UIImage imageNamed:@"nowplaying.png"];
    UIBarButtonItem *nowPlayingButtonItem = [[[UIBarButtonItem alloc] initWithImage:img style:UIBarButtonItemStyleBordered target:delegate action:@selector(presentNowPlayingMovie)] autorelease];
    
    0 讨论(0)
  • 2020-12-16 21:51
    - (void)createCustomRightBarButton_
    {
        UILabel * addCustomLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 64, 25)] autorelease];
        addCustomLabel.text =@"Now\nPlaying";
        addCustomLabel.textColor = [UIColor whiteColor];
        addCustomLabel.font = [UIFont boldSystemFontOfSize:11];
        addCustomLabel.numberOfLines = 2;
        addCustomLabel.backgroundColor = [UIColor clearColor];
        addCustomLabel.textAlignment = UITextAlignmentCenter;
    
        CGSize size = addCustomLabel.bounds.size;
    
        UIGraphicsBeginImageContext(size);      
        CGContextRef context = UIGraphicsGetCurrentContext();       
        [addCustomLabel.layer renderInContext: context];
        CGImageRef imageRef = CGBitmapContextCreateImage(context);
        UIImage * img = [UIImage imageWithCGImage: imageRef];
        CGImageRelease(imageRef);
        CGContextRelease(context);
    
        self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:img
                                                                               style:UIBarButtonItemStyleBordered 
                                                                              target:self 
                                                                              action:@selector(flipView)]
                                                  autorelease];
    }
    
    0 讨论(0)
提交回复
热议问题