UIBarButton Item on screen edge in iOS 8 when used in standalone view

陌路散爱 提交于 2019-12-01 16:10:12

Not sure whats happening with UIBarButtonItem. It should be arrange by auto layout and it has to be work well. May be one of the constraint conflict with other or misguided.

If you are not able to resolve it. I have one more solution for you. You need to create UIBarButtonItem programatically in your VC.

The idea is to assign space from left and right padding before you add UIBarButtonItem.

Below code will guide you to do the trick.

UIBarButtonItem *leftPadding = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
                                UIBarButtonSystemItemFixedSpace target:self action:nil];
[leftPadding setWidth:5];  // Adjust width for padding from left side.

UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
                              UIBarButtonSystemItemAdd target:self action:@selector(addButtonTapped:)];
[self.navigationItem setLeftBarButtonItems:@[leftPadding, addButton]];


UIBarButtonItem *rightPadding = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
                                 UIBarButtonSystemItemFixedSpace target:self action:nil];
[rightPadding setWidth:10]; // Adjust width for padding from right side.

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
                               UIBarButtonSystemItemDone target:self action:@selector(doneButtonTapped:)];

[self.navigationItem setRightBarButtonItems:@[rightPadding, doneButton]];

Add button using the below code, it will definitely work.

CGRect frameimg = CGRectMake(0, 0, 60, 30);
UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];
//[someButton setBackgroundImage:image3 forState:UIControlStateNormal];
[someButton setTitle:@"Update" forState:UIControlStateNormal];
[someButton addTarget:self action:@selector(updateProfile)
     forControlEvents:UIControlEventTouchUpInside];
[someButton setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *menuButton =[[UIBarButtonItem alloc] initWithCustomView:someButton];
self.navigationItem.rightBarButtonItem = menuButton;

The issue is in layoutMargins.

Seems like your UINavigationBar has zero margin. It should have 8 points from each side.

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