2015-06-29 9 views
10

Desidero mostrare il bordo inferiore solo e nascondere gli altri lati.Come visualizzare solo il bordo inferiore di UITextField in Swift

Uscita Vedo: Come potete vedere vedo i bordi superiore, sinistro e destro anch'essi e sono di colore nero, voglio rimuoverli. È necessario solo il bordo 2.0 di spessore inferiore bianco.

enter image description here

codice che sto usando (source):

var border = CALayer() 
var width = CGFloat(2.0) 
border.borderColor = UIColor.whiteColor().CGColor 
border.frame = CGRect(x: 0, y: tv_username.frame.size.height - width, width: tv_username.frame.size.width, height: tv_username.frame.size.height) 

border.borderWidth = width 
tv_username.backgroundColor = UIColor.clearColor() 
tv_username.layer.addSublayer(border) 
tv_username.layer.masksToBounds = true 
tv_username.textColor = UIColor.whiteColor() 

risposta

29

cercare di fare in questo modo.

var bottomLine = CALayer() 
bottomLine.frame = CGRectMake(0.0, myTextField.frame.height - 1, myTextField.frame.width, 1.0) 
bottomLine.backgroundColor = UIColor.whiteColor().CGColor 
myTextField.borderStyle = UITextBorderStyle.None 
myTextField.layer.addSublayer(bottomLine) 

è necessario impostare la proprietà borderStyle-None

Se si utilizza il layout automatico quindi impostare non apparirà perfetto vincolo altro bottomline.

Spero che aiuti.

+0

provato il codice, ma vedo tutto confine scomparire con che questo codice . (il mio background è blu, non sono sicuro che sia importante). Voglio che il bordo inferiore sia solo bianco. – user1406716

+0

@ user1406716 Verifica questo: http://stackoverflow.com/questions/30701655/how-to-get-this-see-inside-type-of-a-uitextfield/30701711#30701711 La mia risposta –

+0

significa che c'è qualcosa di aggiuntivo richiesto oltre le 5 linee sopra? Li ho usati semplicemente sostituendo 'myTextField' con il mio 'tv_username' UITextField – user1406716

4

Objective C

[txt.layer setBackgroundColor: [[UIColor whiteColor] CGColor]]; 
[txt.layer setBorderColor: [[UIColor grayColor] CGColor]]; 
[txt.layer setBorderWidth: 0.0]; 
[txt.layer setCornerRadius:12.0f]; 
[txt.layer setMasksToBounds:NO]; 
[txt.layer setShadowRadius:2.0f]; 
txt.layer.shadowColor = [[UIColor blackColor] CGColor]; 
txt.layer.shadowOffset = CGSizeMake(1.0f, 1.0f); 
txt.layer.shadowOpacity = 1.0f; 
txt.layer.shadowRadius = 1.0f; 

Swift

textField.layer.backgroundColor = UIColor.whiteColor().CGColor 
textField.layer.borderColor = UIColor.grayColor().CGColor 
textField.layer.borderWidth = 0.0 
textField.layer.cornerRadius = 5 
textField.layer.masksToBounds = false 
textField.layer.shadowRadius = 2.0 
textField.layer.shadowColor = UIColor.blackColor().CGColor 
textField.layer.shadowOffset = CGSizeMake(1.0, 1.0) 
textField.layer.shadowOpacity = 1.0 
textField.layer.shadowRadius = 1.0 
2

Ho provato tutti questa risposta, ma nessuno ha funzionato per me tranne questo

let borderWidth:CGFloat = 2.0 // what ever border width do you prefer 
    let bottomLine = CALayer() 

    bottomLine.frame = CGRectMake(0.0, Et_textfield.height - borderWidth, Et_textfield.width, Et_textfield.height) 
    bottomLine.backgroundColor = UIColor.blueColor().CGColor 
    bottomLine 
    Et_textfield.layer.addSublayer(bottomLine) 
    Et_textfield.layer.masksToBounds = true // the most important line of code 
0

Per Campo multipla Testo

override func viewDidLoad() { 


    configureTextField(x: 0, y: locationField.frame.size.height-1.0, width: locationField.frame.size.width, height:1.0, textField: locationField) 
    configureTextField(x: 0, y: destinationField.frame.size.height-1.0, width: destinationField.frame.size.width, height:1.0, textField: destinationField) 
    configureTextField(x: 0, y: originField.frame.size.height-1.0, width: originField.frame.size.width, height:1.0, textField: originField) 
    configureTextField(x: 0, y: nameField.frame.size.height-1.0, width: nameField.frame.size.width, height:1.0, textField: nameField) 

    locationField.text="Hello" 
    super.viewDidLoad() 

    // Do any additional setup after loading the view. 
} 

func configureTextField(x:CGFloat,y:CGFloat,width:CGFloat,height:CGFloat,textField:UITextField) 

{ 
    let bottomLine = CALayer() 
    bottomLine.frame = CGRect(x: x, y: y, width: width, height: height) 
    bottomLine.backgroundColor = UIColor.white.cgColor 
    textField.borderStyle = UITextBorderStyle.none 
    textField.layer.addSublayer(bottomLine) 


} 
0

Swift 3:

Proprio sottoclasse tua UITextField

class BottomBorderTF: UITextField { 

var bottomBorder = UIView() 
override func awakeFromNib() { 

    //MARK: Setup Bottom-Border 
    self.translatesAutoresizingMaskIntoConstraints = false 
    bottomBorder = UIView.init(frame: CGRect(x: 0, y: 0, width: 0, height: 0)) 
    bottomBorder.backgroundColor = UIColor.orange 
    bottomBorder.translatesAutoresizingMaskIntoConstraints = false 
    addSubview(bottomBorder) 
    //Mark: Setup Anchors 
    bottomBorder.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true 
    bottomBorder.leftAnchor.constraint(equalTo: leftAnchor).isActive = true 
    bottomBorder.rightAnchor.constraint(equalTo: rightAnchor).isActive = true 
    bottomBorder.heightAnchor.constraint(equalToConstant: 1).isActive = true // Set Border-Strength 

    } 
}