Advertisement
Guest User

Untitled

a guest
Jul 25th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. class ViewController: UIViewController {
  2.  
  3. var navView: UIView!
  4. var searchBar: UISearchBar!
  5. var leftConstraint: NSLayoutConstraint!
  6. var widthConstraint: NSLayoutConstraint!
  7.  
  8. override func viewDidLoad() {
  9. super.viewDidLoad()
  10.  
  11. // Background view.
  12. navView = UIView()
  13. navView.backgroundColor = UIColor.greenColor()
  14. navView.translatesAutoresizingMaskIntoConstraints = false
  15. navigationItem.titleView = navView
  16. navView.superview!.addConstraint(NSLayoutConstraint(item: navView, attribute: .Top, relatedBy: .Equal, toItem: navView.superview!, attribute: .Top, multiplier: 1, constant: 0))
  17. navView.addConstraint(NSLayoutConstraint(item: navView, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0, constant: UIScreen.mainScreen().bounds.width))
  18. navView.addConstraint(NSLayoutConstraint(item: navView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0, constant: navigationController!.navigationBar.bounds.height))
  19.  
  20. // Search button.
  21. navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Search, target: self, action: #selector(open))
  22.  
  23. // Search bar.
  24. searchBar = UISearchBar()
  25. searchBar.translatesAutoresizingMaskIntoConstraints = false
  26. navView.addSubview(searchBar)
  27.  
  28. navView.addConstraint(NSLayoutConstraint(item: searchBar, attribute: .CenterY, relatedBy: .Equal, toItem: navView, attribute: .CenterY, multiplier: 1, constant: 0))
  29. leftConstraint = NSLayoutConstraint(item: searchBar, attribute: .Left, relatedBy: .Equal, toItem: navigationItem.titleView, attribute: .LeftMargin, multiplier: 1, constant: 0)
  30. leftConstraint.priority = UILayoutPriorityDefaultLow // Starts out as low priority so it has no effect.
  31. navView.addConstraint(leftConstraint)
  32.  
  33. navView.addConstraint(NSLayoutConstraint(item: navigationItem.titleView!, attribute: .Right, relatedBy: .Equal, toItem: searchBar, attribute: .Right, multiplier: 1, constant: 45))
  34. widthConstraint = NSLayoutConstraint(item: searchBar, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0, constant: 10)
  35. widthConstraint.priority = UILayoutPriorityDefaultHigh // Starts out as high priority so it does have effect.
  36. navView.addConstraint(widthConstraint)
  37.  
  38. // Remove search bar border.
  39. searchBar.layer.borderColor = UIColor.greenColor().CGColor
  40. searchBar.layer.borderWidth = 1
  41.  
  42. // Match background color.
  43. searchBar.barTintColor = UIColor.greenColor()
  44.  
  45. // Search bar starts invisible.
  46. searchBar.alpha = 0
  47. }
  48.  
  49. func open() {
  50.  
  51. // Switch the priorities to make search bar expand.
  52. leftConstraint.priority = UILayoutPriorityDefaultHigh
  53. widthConstraint.priority = UILayoutPriorityDefaultLow
  54.  
  55. // Animate change to visible.
  56. UIView.animateWithDuration(2, animations: {
  57. self.searchBar.alpha = 1
  58. self.searchBar.layoutIfNeeded()
  59. })
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement