Last active
August 29, 2015 14:05
UIToolbar Bug iOS8 Beta 5 AND GM - UPDATE: Fix included!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Execute the following code somewhere in your app | |
UIToolbar *datePickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.f, 50.f, self.window.bounds.size.width, 44.f)]; | |
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; | |
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:nil]; | |
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"OK" style:UIBarButtonItemStyleDone target:self action:nil]; | |
datePickerToolbar.items = @[cancelButton, flexibleSpace, doneButton]; | |
[self.window addSubview:datePickerToolbar]; | |
/* | |
* On iOS 8 Beta 5 and Golden Master the buttons will have no horizontal margin to | |
* the UIToolbar's frame when added to an UIWindow. | |
* When added to an UIView there is the typical frame. | |
* | |
* On iOS 8 Beta 1 the buttons have the typical margins also when added to an UIWindow. | |
* | |
* If you look at the images you will see what I mean. | |
*/ | |
/* | |
* UPDATE on 10.09.2014: | |
* ===================== | |
* Fix which needs to check against name of private API class but it's obfuscated | |
* as much as possible. | |
* You need to override UIToolbar as follows: | |
*/ | |
static CGFloat MSCToolbarAppleDefaultPadding = 8.f; | |
@implementation MSCToolbar | |
//////////////////////////////////////////////////////////////////////// | |
#pragma mark - UIView | |
//////////////////////////////////////////////////////////////////////// | |
- (void)layoutSubviews { | |
[super layoutSubviews]; | |
[self.subviews enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop) { | |
/** | |
* Temporarely fix for this bug: | |
* ============================= | |
* https://gist.github.com/scheinem/f579fc01209b033aac91 | |
* | |
*/ | |
if ([NSStringFromClass(view.class) hasPrefix:@"UIToolbar"] && | |
[NSStringFromClass(view.class) hasSuffix:@"Button"]) { | |
CGRect buttonFrame = view.frame; | |
if (buttonFrame.origin.x == 0) { | |
buttonFrame.origin.x = MSCToolbarAppleDefaultPadding; | |
} else if (buttonFrame.origin.x + buttonFrame.size.width == self.bounds.size.width) { | |
buttonFrame.origin.x -= MSCToolbarAppleDefaultPadding; | |
} | |
view.frame = buttonFrame; | |
} | |
}]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just ran across this in a Google search, but in general, adding views directly to the UIWindow is not a good idea -- you would normally want to add as a subview of window.rootViewController.view .