I am trying to implement a custom UIBarButtonItem. I have subclassed it (for other reasons) and I'm having an issue where it's placed correctly in iOS 10 and 11, but completely misplaced in iOS 8 and 9.
This is what it looks like in iOS 10 and 11, and is correct:

This is what it looks like in iOS 8 and 9, and is completely wrong:

The button should display a kind of 'badge' if there are new features available, but I won't be getting into that now. Just don't answer with "You don't need to subclass/wrapperView, you can send customView in init", there are other reasons for this approach. I am simplifying the issue.
In the initialiser for my custom UIBarButtonItem I create one UIButton and a UIView, namely: button and wrapperView.
The wrapperView is a basic UIView which contains the button. The button is constrained to a certain width, and is constrained to fit in the wrapperView.
At the end of my custom initialiser, I set self.customView = wrapperView.
(The wrapperView is wrapping more than just the button, so setting button directly as customView is not an option)
When the custom UIBarButtonItem is finished initialising, it is added to self.navigationItem.rightBarButtonItems = [myButton].
There is a lot of code to make it, but I'll provide what I can:
class MyBarButtonItem:UIBarButtonItem{
let button:UIButton
override init(){
button = UIButton(type: custom)
super.init()
//Set image and add target for button
let wrapperView = UIView()
wrapperView.translatesAutoresizingMaskIntoConstraints = false
button.translatesAutoresizingMaskIntoConstraints = false
wrapperView.addSubview(button)
//constrain button to 0pt from top/leading/bottom/trailing of wrapperView.
//Also constrain button to 26pt width (as wide as my image)
wrapperView.addConstraints([/*myConstraints*/])
button.addConstraint(widthConstraint)
button.clipsToBounds = false
wrapperView.clipsToBounds = false
self.customView = wrapperView
self.style = .plain
}
}
There is a lot more code, but this is still happening when I comment them out. Any idea what's happening here?
As my custom UIBarButtonItem is simply a regular UIBarButtonItem with a simple UIView (with subviews and constraints inside) as customView, and then put in rightBarButtonItems, shouldn't it be easily displayed on the right side?.. Is this an issue due to my constraints?
EDIT
Seems like this is indeed a problem with my constraints. It seems to me like constraints are 'illegal' in customView.. As soon as I remove any constraints, the button is back to where it is supposed to be.. But I'm not sure if I'm completely comfortable without constraints..
But when I now run my code in iOS 10 or 11 it has again been completely moved.. Damnit!