NavigationBarにItem(ボタン)を追加　押したらiPhoneのホーム画面のドックみたいなUIViewを下から表示

#完成形
<img width="150" alt="EE0F3C7E-D57F-4A20-9FED-87CF9964EB5B_1_201_a.jpeg" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1466343/2cb4b00a-09c1-d7a2-bb33-720c744d49b1.jpeg"> <img width="147" alt="F183359C-ECD9-4F8E-85FA-CBA9E5FEF31E_1_201_a.jpeg" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1466343/c7952f78-6541-3225-f4a0-00e7f9ad529f.jpeg">

#機能説明
* NavigationBarの右端に設置したItemを押すと、下からiPhoneのドックみたいなUIViewを表示させます。

#コードと簡単解説

##NavigationBarにItem(ボタン)を追加

* NavigationBarの右端に`.action`アイコンを設定して追加しています。

* `action: #selector(showUnderView)`は、UIViewを上げ下げする為の処理を設定しています。

```Swift:
    override func viewDidLoad() {
       super.viewDidLoad()
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(showUnderView))
        
    }
```

* 0.3秒でUIViewとUIButtonを`animations:{}`で指定した位置まで移動させます。　

```Swift:
           ~~~一部省略~~~

    var underView = UIView()
    var underViewButton = UIButton()

           ~~~一部省略~~~

    @objc func showUnderView(){
        
        if underView.frame.origin.y == self.view.frame.maxY{
            
            UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {self.underView.frame.origin.y = self.view.frame.maxY - (self.view.frame.maxY / 7.5)}, completion: nil)
            
            UIButton.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {self.underViewButton.frame.origin.y = self.underView.frame.origin.y + 10}, completion: nil)
            
        }else if underView.frame.origin.y == self.view.frame.maxY - (self.view.frame.maxY / 7.5){
            
            UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {self.underView.frame.origin.y = self.view.frame.maxY}, completion: nil)

            UIButton.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {self.underViewButton.frame.origin.y = self.underView.frame.origin.y + 3}, completion: nil)
        }
    }
```

# iPhoneのホーム画面のドックみたいなUIViewを作成

* ホーム画面のドックみたいなUIViewを作成

```Swift:
           ~~~一部省略~~~

    var underView = UIView()

           ~~~一部省略~~~

func createUnderView(){
        
        underView = UIView(frame: CGRect(x: view.frame.minX + 5, y: self.view.frame.maxY, width: view.frame.size.width - 10, height: view.frame.size.height / 9))
        
        underView.layer.cornerRadius = 15.0   //背景色を設定
        underView.backgroundColor = .systemGray2
        underView.alpha = 0.35   //透過度を設定
        
        view.addSubview(underView)
        
    }
```

#全てのコード
```Swift:ViewController
import UIKit

class ViewController: UIViewController {

    var underView = UIView()
    var underViewButton = UIButton()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(showUnderView))
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        createUnderView()
        createUnderViewButton(yPoint: underView.frame.origin.y)
    }

    @objc func showUnderView(){
        
        if underView.frame.origin.y == self.view.frame.maxY{
            
            UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {self.underView.frame.origin.y = self.view.frame.maxY - (self.view.frame.maxY / 7.5)}, completion: nil)
            
            UIButton.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {self.underViewButton.frame.origin.y = self.underView.frame.origin.y + 10}, completion: nil)
            
        }else if underView.frame.origin.y == self.view.frame.maxY - (self.view.frame.maxY / 7.5){
            
            UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {self.underView.frame.origin.y = self.view.frame.maxY}, completion: nil)

            UIButton.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: .curveEaseInOut, animations: {self.underViewButton.frame.origin.y = self.underView.frame.origin.y + 3}, completion: nil)
        }
    }
    
    func createUnderView(){
        
        underView = UIView(frame: CGRect(x: view.frame.minX + 5, y: self.view.frame.maxY, width: view.frame.size.width - 10, height: view.frame.size.height / 9))
        
        underView.layer.cornerRadius = 15.0
        underView.backgroundColor = .systemGray2
        underView.alpha = 0.35
        
        view.addSubview(underView)
        
    }
    
    func createUnderViewButton(yPoint:CGFloat){

        underViewButton = UIButton(frame: CGRect(x: underView.frame.minX + 10, y: yPoint + 10, width: underView.frame.size.height - 20, height:underView.frame.size.height - 20))
        
        underViewButton.backgroundColor = UIColor.systemIndigo
        underViewButton.layer.cornerRadius = 15.0
        underViewButton.setTitle("Button", for: .normal)
        underViewButton.setTitleColor(.white, for: .normal)
        underViewButton.addTarget(self, action: #selector(test), for: .touchDown)
        
        view.addSubview(underViewButton)
        print("underViewButton表示")

    }
    
    @objc func test(){
        print("underViewButton押されました")
    }
    
}
```

# 終わり
ご指摘、ご質問などありましたら、コメントまでお願い致します。