15
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Swift】UITextFieldの枠線を消して下線(アンダーライン)をつけてかっこよくする

Last updated at Posted at 2021-02-01

UITextFieldのデフォルトレイアウトの枠線で囲われているのあまりカッコ良くないですよね。
今回はカッコ良くアンダーラインをつけるExtensionを紹介します。

extension UITextField {
    func setUnderLine() {
        // 枠線を非表示にする
        borderStyle = .none
        let underline = UIView()
        // heightにはアンダーラインの高さを入れる
        underline.frame = CGRect(x: 0, y: frame.height, width: frame.width, height: 0.5)
        // 枠線の色
        underline.backgroundColor = .white
        addSubview(underline)
        // 枠線を最前面に
        bringSubviewToFront(underline)
    }
}

設定したいUITextFieldで呼び出せばOK


textField.setUnderLine()

こんな感じでアンダーラインが表示されます!

15
5
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?