0

At very beginning, I want to just create every textField by my self-design function:

func creatTextField(x x0:Int,y y0: Int,w w0: Int,h h0: Int) -> UITextField{
        let x = CGFloat(x0)
        let y = CGFloat(y0)
        let w = CGFloat(w0)
        let h = CGFloat(h0)

        let frame = CGRectMake(0, 0, w, h)
        let center = CGPointMake(x/2, y/2)
        let tf = UITextField(frame: frame)
            tf.center = center
        view.addSubview(tf)

        return tf
    }

and In the viewDidLoad(),I just want to add a new TextField:

override func viewDidLoad() {
        super.viewDidLoad()
        TextField = creatTextField(x: 50, y: 50, w: 100, h: 30)
    }

And nothing changed when I ran the program

I am considering that did I miss something such as CONSTRAIN or I should use STACK VIEW to present those textFields correctly?

I hope someone can help me out! :D

b.t.w. Because it just a test, I just add one textField.But I should be a 2D-Array< UITextField >

Microos
  • 1,728
  • 3
  • 17
  • 34

1 Answers1

2

Just add this line into your code:

tf.borderStyle = UITextBorderStyle.RoundedRect

And final code will be:

func creatTextField(x x0:Int,y y0: Int,w w0: Int,h h0: Int) -> UITextField{
    let x = CGFloat(x0)
    let y = CGFloat(y0)
    let w = CGFloat(w0)
    let h = CGFloat(h0)

    let frame = CGRectMake(0, 0, w, h)
    let center = CGPointMake(x/2, y/2)
    let tf = UITextField(frame: frame)
    tf.center = center
    tf.borderStyle = UITextBorderStyle.RoundedRect
    view.addSubview(tf)

    return tf
}
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
  • Thank you! It works! btw, can you tell me how to implement the autoLayout 's Constrains by code. I am confuse about the apple api – Microos Oct 21 '15 at 06:45
  • 1
    This me help you.. : http://stackoverflow.com/questions/26180822/swift-adding-constraints-programmatically – Dharmesh Kheni Oct 21 '15 at 06:46
  • 1
    i would suggest using a collection view to lay out the textviews instead, then you wont have to mess around with autolayouts in code and creating a mess – Fonix Oct 21 '15 at 06:47
  • 1
    Yes I am agreed with @Fonix. – Dharmesh Kheni Oct 21 '15 at 06:48
  • @Fonix--- I did prepare a Collection to append those new textFields. But I still few confuse about how to implement the auto layout by code. Because I am not sure how many Constraints for each view needed.And don't sure what exactly they are – Microos Oct 21 '15 at 17:38
  • @DharmeshKheni--- I did view the question. But It seems still difficult to pinpoint the position & constraints a programmatically created view by add constraints by code – Microos Oct 21 '15 at 17:40