Search code examples
swiftuitextfieldsprite-kitskscene

Use of unresolved identifier 'UITextBorderStyleRoundedRect' in swift


I am trying to create a text field by code in swift, here is snippet of my code:

let hiddenTextField = UITextField(frame: CGRectMake(0, self.size.height - 30, 50, 10))
hiddenTextField.borderStyle = UITextBorderStyleRoundedRect

Problem is - I am getting this error:

Use of unresolved identifier 'UITextBorderStyleRoundedRect'

Note: I am doing this in subclass of SKScene

Any clues?


Solution

  • As part of the transition to a more flexible language, some of the Swift Cocoa APIs are tweaked from their Objective-C counterparts. You'll definitely notice then when dealing with enums—Swift enums are much more powerful and extensible. What you're looking for is the RoundedRect case of the UITextBorderStyle enum type, which can even be inferred from the left-hand side:

    hiddenTextField.borderStyle = .roundedRect
    

    I'll refer you to the Swift enum documentation, since Swift enums are significantly different from most other languages. You can also create extensions on enums.