Use UIBarButtonItem icon in UIButton
New iOS 13 support SF Symbols nowUIImage(systemName: "trash")
for swift 4.2 (call it on main thread)
extension UIBarButtonItem.SystemItem { func image() -> UIImage? { let tempItem = UIBarButtonItem(barButtonSystemItem: self, target: nil, action: nil) // add to toolbar and render it let bar = UIToolbar() bar.setItems([tempItem], animated: false) bar.snapshotView(afterScreenUpdates: true) // got image from real uibutton let itemView = tempItem.value(forKey: "view") as! UIView for view in itemView.subviews { if let button = view as? UIButton, let image = button.imageView?.image { return image.withRenderingMode(.alwaysTemplate) } } return nil }}
UIBarButtonSystemItem.play.image()
For Objective-C:
+ (UIImage *)imageFromSystemBarButton:(UIBarButtonSystemItem)systemItem { UIBarButtonItem* tempItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:systemItem target:nil action:nil]; // Add to toolbar and render it UIToolbar *bar = [[UIToolbar alloc] init]; [bar setItems:@[tempItem] animated:NO]; [bar snapshotViewAfterScreenUpdates:YES]; // Get image from real UIButton UIView *itemView = [(id)tempItem view]; for (UIView* view in itemView.subviews) { if ([view isKindOfClass:[UIButton class]]) { return [(UIButton*)view imageForState:UIControlStateNormal]; } } return nil;}
Here's a solution that works with ANY System bar button item + it supports tintColor
:
- (void)viewDidLoad { [super viewDidLoad]; [self.button setImage:[self imageFromSystemBarButton:UIBarButtonSystemItemTrash] forState:UIControlStateNormal]; self.button.tintColor = [UIColor redColor];}- (UIImage *)imageFromSystemBarButton:(UIBarButtonSystemItem)systemItem { // Holding onto the oldItem (if any) to set it back later // could use left or right, doesn't matter UIBarButtonItem *oldItem = self.navigationItem.rightBarButtonItem; UIBarButtonItem *tempItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:systemItem target:nil action:nil]; // Setting as our right bar button item so we can traverse its subviews self.navigationItem.rightBarButtonItem = tempItem; // Don't know whether this is considered as PRIVATE API or not UIView *itemView = (UIView *)[self.navigationItem.rightBarButtonItem performSelector:@selector(view)]; UIImage *image = nil; // Traversing the subviews to find the ImageView and getting its image for (UIView *subView in itemView.subviews) { if ([subView isKindOfClass:[UIImageView class]]) { image = ((UIImageView *)subView).image; break; } } // Setting our oldItem back since we have the image now self.navigationItem.rightBarButtonItem = oldItem; return image;}
P.S. Feel free to improve if you know of a better way, thanks.
Download the image from somewhere on the web, add it to your project and set the UIButton
's image to the image you just downloaded.
I did not find the same as Apple is using but I found this one. Simply change it's color in Pixelmator or Photoshop.