Change position of UIBarButtonItem in UINavigationBar

后端 未结 20 1346
一个人的身影
一个人的身影 2020-11-27 10:31

How can I change the position of a UIBarButtonItem in a UINavigationBar? I would like my button to be about 5px higher than its normal position.

相关标签:
20条回答
  • 2020-11-27 10:56

    The best solution I could find is to initialize a UIBarButtonItem with a subview that includes extra space to the left/right. That way you wont have to worry about subclassing, and changing the layout of other elements inside the navigation bar, such as the title.

    For example, to move a button 14 points to the left:

    UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, image.size.width + 14, image.size.height)];
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(-14, 0, image.size.width, image.size.height);
    [button setImage:image forState:UIControlStateNormal];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [containerView addSubview:button];
    
    UIButton* button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    button2.frame = CGRectMake(0, 0, image.size.width + 14, image.size.height);
    [button2 addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [containerView addSubview:button2];
    
    UIBarButtonItem* item = [[[self alloc] initWithCustomView:containerView] autorelease];
    
    0 讨论(0)
  • 2020-11-27 10:56

    In my case

    1. change barbuttonItem's frame to customize spaces

    2. Add, Remove barButtonItems dynamically.

    3. change tint colors by tableview's contentOffset.y

    like this

    If your minimum target is iOS 11, you can change the barButton frames in the viewDidLayoutSubviews

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
    
        // Change the navigationBar item frames
        if let customView = wishButton.customView?.superview {
            customView.transform = CGAffineTransform(translationX: 7.0, y: 0)
        }
    
        if let customView = gourmetCountButton.customView?.superview {
            customView.transform = CGAffineTransform(translationX: 9.0, y: 0)
        }
    }
    

    But, it's Only worked on iOS 11.

    I also tried using the fixedSpace. But It didn't work in multiple navigationBarButton items.

     let space = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
        space.width = -10
    

    So, I changed customView's width to adjust horizontal space.

    This is one of the my barButtonItem class

    final class DetailShareBarButtonItem: UIBarButtonItem {
    
    // MARK: - Value
    // MARK: Public
    ***// Change the width to adjust space***
    let button = UIButton(frame: CGRect(x: 0, y: 0, width: 32.0, height: 30.0))
    
    override var tintColor: UIColor? {
        didSet {
            button.tintColor = tintColor
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setButton()
    }
    
    required override init() {
        super.init()
        setButton()
    }
    
    
    // MARK: - Function
    // MARK: Private
    private func setButton() {
        // Button
        button.setImage( #imageLiteral(resourceName: "navibarIcShare02White").withRenderingMode(.alwaysTemplate), for: .normal)
        button.tintColor              = .white
        button.imageEdgeInsets        = UIEdgeInsetsMake(0, 1.0, 1.0, 0)
        button.imageView?.contentMode = .scaleAspectFill
    
        let containerView = UIView(frame: button.bounds)
        containerView.backgroundColor = .clear
        containerView.addSubview(button)
        customView = containerView
    }
    }
    

    This is the result.

    I tested on iOS 9 ~ 11, (Swift 4)

    0 讨论(0)
  • 2020-11-27 10:57

    For those of you developing for iOS 5 who stumbled across this and were discouraged... Try something like this:

    float my_offset_plus_or_minus = 3.0f;
    
    UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithTitle:@"title" 
                                                              style:UIBarButtonItemStyleDone
                                                              target:someObject action:@selector(someMessage)];
    
    [item setBackgroundVerticalPositionAdjustment:my_offset_plus_or_minus forBarMetrics:UIBarMetricsDefault];
    
    0 讨论(0)
  • 2020-11-27 10:57

    I needed to set my button to be more over towards the right. Here's how I did it using UIAppearance in Swift. There's a vertical position property there as well so I imagine you can adjust in any direction.

    UIBarButtonItem.appearance().setTitlePositionAdjustment(UIOffset.init(horizontal: 15, vertical: 0), forBarMetrics: UIBarMetrics.Default)
    

    This seems much less invasive to me than messing with the frame directly or adding custom subviews.

    0 讨论(0)
  • 2020-11-27 11:01

    You can always do adjustments using Insets on the button. For example,

    UIButton *toggleBtn =  [UIButton buttonWithType:UIButtonTypeCustom];
    [toggleBtn setFrame:CGRectMake(0, 0, 20, 20)];
    [toggleBtn addTarget:self action:@selector(toggleView) forControlEvents:UIControlEventTouchUpInside];
    
    [toggleBtn setImageEdgeInsets:((IS_IPAD)? UIEdgeInsetsMake(0,-18, 0, 6) : UIEdgeInsetsMake(0, -3, 0, -3))];
    
    UIBarButtonItem *toggleBtnItem = [[UIBarButtonItem alloc] initWithCustomView: toggleBtn];
    self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:searchBtnItem, toggleBtnItem, nil];
    

    It works for me.

    0 讨论(0)
  • 2020-11-27 11:03

    I found the solution of this problem by making adjustment in the Image Edge Insets of the custom button. I had the requirement in the app to increase the height of the navigation bar and after increasing the height makes the rightBarButtonItem and leftBarButtonItem images unpositioned problem.

    Find the code below:-

    UIImage *image = [[UIImage imageNamed:@"searchbar.png"];
    UIButton* searchbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    [searchbutton addTarget:self action:@selector(searchBar:) forControlEvents:UIControlEventTouchUpInside]; 
    searchbutton.frame = CGRectMake(0,0,22, 22);
    [searchbutton setImage:image forState:UIControlStateNormal];
    [searchbutton setImageEdgeInsets:UIEdgeInsetsMake(-50, 0,50, 0)];
    // Make BarButton Item
     UIBarButtonItem *navItem = [[UIBarButtonItem alloc] initWithCustomView:searchbutton];
    self.navigationItem.rightBarButtonItem = navItem;
    

    Hope this helps anyone.

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