Adjusting navigationItem.titleView's frame?

前端 未结 13 1556
小蘑菇
小蘑菇 2021-01-31 04:14

I noticed that the titleView\'s position depends on the rightbarbutton title. How can I set the frame of the titleView to be in the center instead?

I\'ve tried setting

相关标签:
13条回答
  • 2021-01-31 04:49

    Only way I found to Fix this is to not use UINavigationItems. Insted use two different views on eace button side of the UINavigationBar like so:

    -(UIView *)rightButtonViewSection {
        if (!_rightButtonViewSection) {
    
            _rightButtonViewSection = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 45)];
    
        }
        return _rightButtonViewSection;
    
    }
    
    -(UIView *)leftButtonViewSection {
        if (!_leftButtonViewSection) {
    
            _leftButtonViewSection = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 45)];
    
        }
        return _leftButtonViewSection;
    
    }
    

    and setButtons on it like so:

     ButtonBase *searchButton = [[ButtonBase alloc] initWithFrame:CGRectMake(0, 0, 32, 30)];
        [searchButton setImage:[UIImage imageNamed:SEARCH_IMAGE] forState:UIControlStateNormal];
        SEL searchSelector = @selector(tradersSearchButtonPress);
        [searchButton addTarget:self action:searchSelector forControlEvents:UIControlEventTouchUpInside];
    
        self.notificationButton.view.frame = [self rightButtonFrame];
        searchButton.frame = [self rightSecoundButtonFrame];
    
        [self.rightButtonViewSection removeAllSubviews];
        [self.rightButtonViewSection addSubview:self.notificationButton.view];
        [self.rightButtonViewSection addSubview:searchButton];
    
        UIBarButtonItem *buttonItem  = [[UIBarButtonItem alloc] initWithCustomView:self.rightButtonViewSection];
        self.topViewController.navigationItem.rightBarButtonItem = buttonItem;
    

    ButtonsFrames:

    -(CGRect) rightButtonFrame {
        return CGRectMake(self.rightButtonViewSection.width - 32, 7, 32, 30);
    }
    -(CGRect) rightSecoundButtonFrame {
        return CGRectMake(self.rightButtonViewSection.width - 80, 7, 32, 30);
    }
    

    Then the title will not move! because the UIButtonsViews are same width.

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