So I am having the same problem that many others are experiencing when creating a UIBarButtonItem with a UIButton as a custom view.
Basically the button is about 10 pixel to far either left or right. When I use a regular BarButtonItem without a custom view, this does not happen.
This post provided a partial solution:
UIBarButton With Custom View
Here is my code I have created by subclassing UIButton (as stated in the other post)
- (UIEdgeInsets)alignmentRectInsets {
UIEdgeInsets insets;
if ([self isLeftButton]) {
insets = UIEdgeInsetsMake(0, 9.0f, 0, 0);
}
else { // IF ITS A RIGHT BUTTON
insets = UIEdgeInsetsMake(0, 0, 0, 9.0f);
}
return insets;
}
- (BOOL)isLeftButton {
return self.frame.origin.x < (self.superview.frame.size.width / 2);
}
This works great, but when I pop a view controller from the navigation controller back to this main view, the button is still incorrectly positioned for about .3 seconds, and then it snaps into the correct inset.
This is a HUGE eyesore and I have no idea how to stop it from snapping like so. Any thoughts? Thanks!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…