swiftでよく使うソースコードによる装飾
swiftでよく使うソースコードによる装飾をメモし続ける。随時追加していく。
.textが空だったら、アラート。 .isEmpty を使う
  if hogeField.text!.isEmpty {
        //アラートの処理
    } else {
        let hoge = Hoge()
        hoge.title = hogeField.text!
    }
ナビゲーションコントロールバーの記述例
  @IBAction func tapTransitionBtn(_ sender: UIButton) {
        self.performSegue(withIdentifier: "NextSegue", sender: nil)
    }
    
    //viewWillAppearは、そのviewControllerが読みこまれるたびに実行
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        //ナビゲーションバーの色
        let deepGreen = UIColor(red: 72.0 / 255, green: 140.0 / 255, blue: 141.0 / 255, alpha: 1.0)
        self.navigationController!.navigationBar.barTintColor = deepGreen
        self.title = "Main View Controller"
        
        //ヘッダーナビのタイトルへの設定
        self.navigationController!.navigationBar.titleTextAttributes =
            [NSAttributedStringKey.foregroundColor: UIColor.white,
             NSAttributedStringKey.font: UIFont(name: "Helvetica-Bold", size: 20)!]
        
        //Navigation Barに配置したボタンの色
        self.navigationController!.navigationBar.tintColor = UIColor.white
        
        //曇りガラスをオフ
        self.navigationController!.navigationBar.isTranslucent = false
        
        //ナビゲーションバー 右端にnextを入れる
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Next", style: UIBarButtonItemStyle.plain, target: self, action: #selector(ViewController.toNextViewController(sender:)))
    }
    
    //nextをタップした時の処理
    @objc func toNextViewController(sender: UIBarButtonItem) {
        self.performSegue(withIdentifier: "NextSegue", sender: nil)
    }
ナビゲーションコントローラーのヘッダーバーの背景
 override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        let deepGreen = UIColor(red: 72.0 / 255, green: 140.0 / 255, blue: 141.0 / 255, alpha: 1.0)
        self.navigationController!.navigationBar.barTintColor = deepGreen
    }
  
ボタンをタップした時のセグエ処理の一例
  override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let button = UIButton()
        button.addTarget(self, action: #selector(ViewController.didTouchButton(sender:)), for: UIControlEvents.touchUpInside)
        self.view.addSubview(button)
    }
    @objc func didTouchButton(sender: UIButton) {
        self.performSegue(withIdentifier: "ModalSegue", sender: self)
    }
labelの底を基準に配置するのは.maxYなどが便利。0だと一番下。
button.center = CGPoint(x: self.view.center.x, y: label.frame.maxY + 30)
文字量によってサイズが可変する
button.sizeToFit()
labelなどを、画面の外に配置する
label.frame.origin = CGPoint(x: self.view.frame.width, y: 200)
labelやbuttonを角まるで正円にする
hogeLabel.layer.cornerRadius = hogeLabel.frame.width / 2 hogeLabel.layer.masksToBounds = true
ボーダーを設定する
    //ボーダの幅を指定
    hogeImageView.layer.borderWidth = 1
    //ボーダーの色を指定
    hogeImageView.layer.borderColor = UIColor.white.cgColor
textViewの基本記述例
//テキスト内容 hogeTextView.text = "Go hogeSite !!!\nhttps://okadalabo.com/" //テキストカラー hogeTextView.textColor = UIColor.gray //背景色 hogeTextView.backgroundColor = UIColor(red: 0.5, green: 0.7, blue: 0.3, alpha: 1.0) //フォント hogeTextView.font = UIFont(name: "Helvetica", size: 18) // リンクになり得る全ての要素をリンクにする。 hogeTextView.dataDetectorTypes = UIDataDetectorTypes.all //編集不可 hogeTextView.isEditable = false //パディングの設定 hogeTextView.textContainerInset = UIEdgeInsets(top: 15, left: 10, bottom: 0, right: 0)
textfieldの基本記述例
//テキストの設定 hogeTextField.text = "hello world" //フォントカラー hogeTextField.textColor = UIColor.blue //フォント、フォントサイズ hogeTextField.font = UIFont(name: "Helvetica-Bold", size: 30) //フォントサイズのみ hogeTextField.font = UIFont.systemFont(ofSize: 30) //整列 hogeTextField.textAlignment = NSTextAlignment.center // プレイスホルダの設定 hogeTextField.placeholder = "キーワードで検索" // クリアボタンの設置 hogeTextField.clearButtonMode = UITextFieldViewMode.always //編集モードになったら、入力されているテキストを消去 hogeTextField.clearsOnBeginEditing = true //空欄の状態ではreturnkeyがグレイアウトする。 hogeTextField.enablesReturnKeyAutomatically = true //パスワード入力モード hogeTextField.isSecureTextEntry = true
labelの基本記述例
hogeLabel.text = "Swift\nis\ngreat!!" // \n は改行 hogeLabel.textColor = UIColor.blue hogeLabel.font = UIFont(name: "HiraginoSans-W6", size: 15) hogeLabel.textAlignment = NSTextAlignment.right hogeLabel.numberOfLines = 2 //座標で配置 hogeLabel.frame.origin = CGPoint(x: 130, y: 200) //表示させるテキストとフォントサイズによってlabel自体の大きさを決めるメソッド hogeLabel.sizeToFit()
buttonの基本記述例
hogeButton.setTitle("tap", for: UIControlState.normal)
hogeButton.setTitle("no tap", for: UIControlState.disabled)
hogeButton.setTitle("tapping", for: UIControlState.highlighted)
hogeButton.setTitleColor(UIColor.red, for: UIControlState.highlighted)
hogeButton.titleLabel!.font = UIFont(name: "Helvetica-Bold", size: 30)
buttonの基本記述例2
//Buttonの生成
let button = UIButton()
//Buttonの位置とサイズの設定
button.frame = CGRect(x: 10, y: 30, width: 80, height: 80)
//Buttonの背景画像の設定
button.setBackgroundImage(UIImage(named: "hoge.png"), for: UIControlState.normal)
//Buttonの角を丸くする
button.layer.cornerRadius = 3.0
//Buttonの背景画像をButtonのサイズで切り取る
button.clipsToBounds = true
//Buttonをタップ時にtappedButtonという名前のアクションを呼ぶ
button.addTarget(self, action: #selector(ViewController.tappedButton(_:)), for: .touchUpInside)
//ButtonをScrollViewに配置
myScrollView.addSubview(button)
//Buttonをタップした時に呼ばれるメソッド
@objc func tappedButton(_ sender: UIButton){
    print("Buttonタップ")
}
      


