Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ViewController: UIViewController {
- var navView: UIView!
- var searchBar: UISearchBar!
- var leftConstraint: NSLayoutConstraint!
- var widthConstraint: NSLayoutConstraint!
- override func viewDidLoad() {
- super.viewDidLoad()
- // Background view.
- navView = UIView()
- navView.backgroundColor = UIColor.greenColor()
- navView.translatesAutoresizingMaskIntoConstraints = false
- navigationItem.titleView = navView
- navView.superview!.addConstraint(NSLayoutConstraint(item: navView, attribute: .Top, relatedBy: .Equal, toItem: navView.superview!, attribute: .Top, multiplier: 1, constant: 0))
- navView.addConstraint(NSLayoutConstraint(item: navView, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0, constant: UIScreen.mainScreen().bounds.width))
- navView.addConstraint(NSLayoutConstraint(item: navView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0, constant: navigationController!.navigationBar.bounds.height))
- // Search button.
- navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Search, target: self, action: #selector(open))
- // Search bar.
- searchBar = UISearchBar()
- searchBar.translatesAutoresizingMaskIntoConstraints = false
- navView.addSubview(searchBar)
- navView.addConstraint(NSLayoutConstraint(item: searchBar, attribute: .CenterY, relatedBy: .Equal, toItem: navView, attribute: .CenterY, multiplier: 1, constant: 0))
- leftConstraint = NSLayoutConstraint(item: searchBar, attribute: .Left, relatedBy: .Equal, toItem: navigationItem.titleView, attribute: .LeftMargin, multiplier: 1, constant: 0)
- leftConstraint.priority = UILayoutPriorityDefaultLow // Starts out as low priority so it has no effect.
- navView.addConstraint(leftConstraint)
- navView.addConstraint(NSLayoutConstraint(item: navigationItem.titleView!, attribute: .Right, relatedBy: .Equal, toItem: searchBar, attribute: .Right, multiplier: 1, constant: 45))
- widthConstraint = NSLayoutConstraint(item: searchBar, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0, constant: 10)
- widthConstraint.priority = UILayoutPriorityDefaultHigh // Starts out as high priority so it does have effect.
- navView.addConstraint(widthConstraint)
- // Remove search bar border.
- searchBar.layer.borderColor = UIColor.greenColor().CGColor
- searchBar.layer.borderWidth = 1
- // Match background color.
- searchBar.barTintColor = UIColor.greenColor()
- // Search bar starts invisible.
- searchBar.alpha = 0
- }
- func open() {
- // Switch the priorities to make search bar expand.
- leftConstraint.priority = UILayoutPriorityDefaultHigh
- widthConstraint.priority = UILayoutPriorityDefaultLow
- // Animate change to visible.
- UIView.animateWithDuration(2, animations: {
- self.searchBar.alpha = 1
- self.searchBar.layoutIfNeeded()
- })
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement