title: Changing the color of the icons in a UItextField inside a UISearchBar
body: <p>I'm trying to customise the appearance of the search bar in my search controller.</p>
  
  <p>Setting the background and text colors works fine but I just didn't find a way to change the color of the icons in the text field, specifically the magnifying glass and the x button.</p>
  
  <p>I've found this Objective-C code which should do what I want but I'm struggling to translate it to Swift:</p>
  
  <p><strong><em>(EDIT: Skip to the first answer for the working Swift 3 solution.)</em></strong></p>
  
  <pre><code>UITextField *searchBarTextField = [self.searchDisplayController.searchBar valueForKey:@"_searchField"];
  
  // Magnifying glass icon.
  UIImageView *leftImageView = (UIImageView *)searchBarTextField.leftView;
  leftImageView.image = [LeftImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
  leftImageView.tintColor = [UIColor whiteColor];
  
  // Clear button
  UIButton *clearButton = [searchBarTextField valueForKey:@"_clearButton"];
  [clearButton setImage:[clearButton.imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
  clearButton.tintColor = [UIColor whiteColor];
  </code></pre>
  
  <p>My attempt to translate to Swift:</p>
  
  <pre><code>let textField = searchController.searchBar.valueForKey("searchField") as! UITextField
  // These two work fine.
  textField.backgroundColor = UIColor.blackColor()
  textField.textColor = UIColor.blackColor()
  
  var glassIcon = textField.leftView
  // This would work.
  //glassIcon.hidden = true
  
  // This does not have any effect.
  //glassIcon?.tintColor = UIColor.whiteColor()
  
  // My attempt to translate, but it gives an error.
  glassIcon.image? = UIImage.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
  
   var clearButton = textField.valueForKey("clearButton")!
              clearButton.setImage(clearButton.imageWithRenderingMode(.AlwaysTemplate), forState: .Normal)
  // This gives the error: "Cannot assign to property: 'clearButton' is immutable
  clearButton.tintColor = UIColor.whiteColor()
    // Sorry for the weird formatting, it glitches here in the editor. 
  </code></pre>
  
  <p>The leftView does not seem to have an image property. How can I access that property as the Objective-C code does?
  Also, if there is a better to achieve what I want please let me know.</p>