Can I change the position of navigationbar item?

前端 未结 7 896
无人共我
无人共我 2021-01-31 18:57

Here is the snapshot: \"enter

And the code is here:

UIButton *leftButton =         


        
相关标签:
7条回答
  • 2021-01-31 19:39

    Add a UIBarButtonSystemItemFlexibleSpace item in between the items you want to separate.

    0 讨论(0)
  • 2021-01-31 19:39

    I had a similar issue and found an option that sufficed in the storyboard. Just click on the navbar item and you should see the x/y position options.

    0 讨论(0)
  • 2021-01-31 19:45

    You can use this code,I think this will be helpful to you. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(0,0,50,32); [button setBackgroundImage:[UIImage imageNamed:@"btn_back.png"] forState:UIControlStateNormal];

    [button addTarget:self action:@selector(BackBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 32)];
    backButtonView.bounds = CGRectOffset(backButtonView.bounds, -1, -4);
    [backButtonView addSubview:button];
    
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButtonView];
    //barButtonItem.imageInsets = UIEdgeInsetsMake(-6, 20, 30, 0);
    
    [self.navigationItem setLeftBarButtonItem:barButtonItem];
    
    0 讨论(0)
  • 2021-01-31 19:46

    The height of a standard bar button is 29pt. Do yourself a favor and just shrink leftbutton's height and navi_register_up.png's height instead.

    0 讨论(0)
  • 2021-01-31 19:50

    You need to create a UIBarButtonItem with system item UIBarButtonSystemItemFixedSpace with -5 width which will remove 5 px padding. Then set leftBarButtonItems with both fixed space item and your back bar item like this -

    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    [negativeSpacer setWidth:-5];
    
    self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacer,leftBarItem,nil];
    
    0 讨论(0)
  • 2021-01-31 19:50

    Here's a Swift 3 version of the most voted answer:

     let soundButton = UIBarButtonItem(image:image , style: UIBarButtonItemStyle.plain, target: self, action: #selector(self.soundPressed))
    
            let negativeSpacer = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
            negativeSpacer.width = -5
    
            self.navigationItem.rightBarButtonItems = [negativeSpacer,soundButton]
    
    0 讨论(0)
提交回复
热议问题