Barbutton item in navigtion bar leftbar button got stick to the top left conrner

醉酒当歌 提交于 2019-12-11 13:53:08

问题


bar button got stick to the y origin as 0 to navigation bar and x as 0.but i want to add some y position to barbutton item.

 UIBarButtonItem *barbutton;

            barbutton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(onbackbtnpressed:)];
            [barbutton setWidth:30];
            [barbutton setTintColor:KANEKA_BLUE_COLOR];
            [barbutton setAccessibilityFrame:CGRectMake(10, 10, 30, 20)];
            self.navigationItem.leftBarButtonItem = barbutton;

Please tell me how can i set the barbutton frame or how to set the bar button y position.


回答1:


My views about this question - As per iOS Human Interface Guidelines it is not recommended to add y spacing for the bar button items on navigation bar.

We can update x positioning by setting an array of buttons for navigation item as shown below:

  UIBarButtonItem *fixedSpaceBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
  [fixedSpaceBarButton setWidth:10];

  UIBarButtonItem *barbutton;

  barbutton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(onbackbtnpressed:)];
  [barbutton setWidth:30];
  [barbutton setTintColor:[UIColor redColor]];
  [barbutton setAccessibilityFrame:CGRectMake(10, 10, 30, 20)];

  self.navigationItem.leftBarButtonItems = [NSArray
                                            arrayWithObjects:fixedSpaceBarButton, barbutton, nil];



回答2:


Actually I have managed to set the y origin in little bit different way may be it is big procedure but it is quite good.

I have added a toolbar to viewcontroller and set the bar button item and hidden the navigation bar.And it works for all the popover controllers.

self.toolbar.hidden = NO;
        self.contentSizeForViewInPopover = CGSizeMake(self.view.frame.size.width-100,self.view.frame.size.height-100);
        self.navigationController.navigationBarHidden = YES;
        [self.toolbar setTintColor:KANEKA_BLUE_COLOR];
        [self.backbutton setTintColor:KANEKA_BLUE_COLOR];


来源:https://stackoverflow.com/questions/19509689/barbutton-item-in-navigtion-bar-leftbar-button-got-stick-to-the-top-left-conrner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!