Can't set titleView in the center of navigation bar because back button

前端 未结 10 1508
情话喂你
情话喂你 2020-12-08 02:25

I\'m using an image view to display an image in my nav bar. The problem is that I can\'t set it to the center correctly because of the back button. I checked the related que

相关标签:
10条回答
  • 2020-12-08 02:46

    Don't use titleView.

    Just add your image to navigationController.navigationBar

    CGRect myImageS = CGRectMake(0, 0, 44, 44);
    UIImageView *logo = [[UIImageView alloc] initWithFrame:myImageS];
    [logo setImage:[UIImage imageNamed:@"color.png"]];
    logo.contentMode = UIViewContentModeScaleAspectFit;
    logo.center = CGPointMake(self.navigationController.navigationBar.width / 2.0, self.navigationController.navigationBar.height / 2.0);
    logo.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
    [self.navigationController.navigationBar addSubview:logo];
    
    0 讨论(0)
  • 2020-12-08 02:47

    Extending Darren's answer, a fix for me was to return a sizeThatFits with the UILabel size. It turns out that this is called after layoutSubViews so the label has a size.

    override func sizeThatFits(_ size: CGSize) -> CGSize {
        return CGSize(width: titleLabel.frame.width + titleInset*2, height: titleLabel.frame.height)
    }
    

    Also note that I have + titleInset*2 because Im setting the horizontal constraints like so:

    titleLabel.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: titleInset),
    titleLabel.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -titleInset)
    
    0 讨论(0)
  • 2020-12-08 02:48

    Qun Li's worked perfectly for me. Here's the swift 2.3 code:

    override var frame: CGRect {
        set(newValue) {
            super.frame = newValue
    
            if let superview = self.superview {
                self.center = CGPoint(x: superview.center.x, y: self.center.y)
            }
        }
    
        get {
            return super.frame
        }
    }
    
    0 讨论(0)
  • 2020-12-08 02:52

    I suggest you Override the function - (void)setFrame:(CGRect)fram like this:

    - (void)setFrame:(CGRect)frame  { 
    
        [super setFrame:frame]; //systom function
    
        self.center = CGPointMake(self.superview.center.x, self.center.y);   //rewrite function 
    
    }
    

    so that the titleView.center always the right location

    0 讨论(0)
  • 2020-12-08 02:52

    1) You can try setting your image as UINavigationBar's background image by calling

    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"color.png"] forBarMetrics:UIBarMetricsDefault];
    

    inside the viewDidLoad method.

    That way it will be always centered, but if you have back button with long title as left navigation item, it can appear on top of your logo. And you should probably at first create another image with the same size as the navigation bar, then draw your image at its center, and after that set it as the background image.

    2) Or instead of setting your image view as titleView, you can try simply adding at as a subview, so it won't have the constraints related to right and left bar button items.

    0 讨论(0)
  • 2020-12-08 02:54

    In Swift, this is what worked for me however it´s not the best solution (basically, add it up to navigationBar):

        let titleIV = UIImageView(image: UIImage(named:"some"))
            titleIV.contentMode = .scaleAspectFit
            titleIV.translatesAutoresizingMaskIntoConstraints = false
    
          if let navigationController = self.navigationController{
                navigationController.navigationBar.addSubview(titleIV)
                titleIV.centerXAnchor.constraint(equalTo:  
    navigationController.navigationBar.centerXAnchor).isActive = true
                titleIV.centerYAnchor.constraint(equalTo: navigationController.navigationBar.centerYAnchor).isActive = true
          }
          else{
                view.addSubview(titleIV)
                titleIV.topAnchor.constraint(equalTo: view.topAnchor, constant: UIApplication.shared.statusBarFrame.height).isActive = true
                titleIV.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    
          }
    
    0 讨论(0)
提交回复
热议问题