Implement Swift UITextField in code

Try implementing UITextField in code

If you look at the introductory book on application development, it's just a way to drag and drop the view onto the storyboard. That's fine, but for those who want to go one step further, I will write an article on how to implement the code. First, let's define the variables.

let textField = UITextField()

Nothing is displayed even if you build in this state. Well, it's only natural to declare a variable. Next, let's put it in the view of the view controller.


You can now add it. Let's build it. Simulator Screen Shot - iPod touch (7th generation) - 2021-01-19 at 16.23.12.png that? Nothing is displayed. Actually, there is this, but the width and height are 0. Let's specify the width and height.

// .init(x-axis position,Position in the y-axis direction,width,height)
textField.frame = .init(x: 0, y: 0, width: 200, height: 40)
//Displayed in the middle of the screen =

When you run it with this ~ It seems that there is nothing at first glance, but you can edit it by tapping the middle. Let's also specify placeholder.

textField.placeholder = "Enter here"

You can see that it is displayed properly. But it's hard to understand that there is no frame. Let's add a frame.

textField.layer.borderWidth = 1
textField.layer.borderColor =

Simulator Screen Shot - iPod touch (7th generation) - 2021-01-19 at 16.33.47.png You're done! Let's make it look a little better.

//Round the corners
textField.layer.cornerRadius = 5
//The border line and the characters are too close, so separate them a little
textField.leftView = UIView(frame: .init(x: 0, y: 0, width: 5, height: 0))
textField.leftViewMode = .always

Build with this! Simulator Screen Shot - iPod touch (7th generation) - 2021-01-19 at 16.37.43.png The corners are rounded nicely and there is a gap between the border line and the letters.

This and that of textField

//Turn off the function to correct English spelling without permission
textField.autocorrectionType = .no
//Turn off capitalizing the first letter of English without permission
textField.autocapitalizationType = .none
//Change font size
textField.font = .systemFont(ofSize: 18)
//Put textField in edit mode
//Release textField edit mode

Please use it according to what you want to do. That's all for this time. Thank you for reading.

Recommended Posts

Implement Swift UITextField in code
Implement CustomView in code
[Beginner] Implement NavigationBar in code
How to implement UICollectionView in Swift with code only
[Swift] Create UIButton class in code
[Swift] Implement swipe processing
Division becomes 0 in Swift
Multidimensional array in Swift
Implement markdown in Rails
Implement application function in Rails
Implement follow function in Rails
Code entry method in Qiita
Implement two-step verification in Java
Photo library in Swift UI
Implement Basic authentication in Java
How to implement infinite scrolling (page nate) in Swift TableView
Implement math combinations in Java
2 Implement simple parsing in Java
Java in Visual Studio Code
[Android] Implement Adblock in WebView
Implement Email Sending in Java
Write Java8-like code in Java8
[Swift5] How to avoid applying dark mode (dark appearance) in code
Implement import process in Rails
Implement functional quicksort in Java
Implement writing and reading to Property List (.plist) in Swift
Implement rm -rf in Java.
[Swift] Implement UITableView using xib
Implement XML signature in Java
Implement Table Driven Test in Java 14
Implement textField focus-out event in JavaFX
Guess the character code in Java
Make an FPS counter in Swift
3 Implement a simple interpreter in Java
Try to implement Yubaba in Ruby
Java Spring environment in vs Code
[React Native] Write Native Module in Swift
Specify multiple sort conditions in Swift
Implement simple login function in Rails
Implement tagging function in form object
Implement reCAPTCHA v3 in Java / Spring
Implement Material Design View in Kotlin
Handle C char ** well in Swift
Implement PHP implode function in Java
Implement a gRPC client in Ruby
Division becomes 0 in Swift
[Swift 5] Implement UI for review function
Implementing side menus in Swift UI
Time stamps in nanoseconds even in Swift!
Implement REST API in Spring Boot
Implement Spring Boot application in Gradle
Try to implement Yubaba in Java
Implement CSV download function in Rails
1 Implement simple lexical analysis in Java