swift2 使用swift 2时只有上边框和下边框的文本字段

but5z9lq  于 2022-11-06  发布在  Swift
关注(0)|答案(6)|浏览(195)

我有一个忘记密码的页面。它只有一个文本字段,要求用户填写他们的电子邮件地址。设计师设计的文本字段只有顶部和底部边框。我尝试从这里回答UITextField Only Top And Bottom Border,但在结果中,它只显示文本字段的底部边框。

像在图像中,我想创建一个灰色的边界顶部和底部

wribegjk

wribegjk1#

要删除视图的冲突,您可以创建一个tableView,其中包含一个包含TextField的静态单元格。瞧,完成了......顶部和底部边框是免费的,您将使用标准的苹果工具:)
如果你真的想画层比按照你的链接问题的步骤:

CALayer *topBorder = [CALayer layer];
topBorder.frame = CGRectMake(0, 0, self.frame.size.width, 1);
topBorder.backgroundColor = [UIColor blackColor].CGColor;
[myTextField.layer addSublayer:topBorder];

CALayer *bottomBorder = [CALayer layer];
bottomBorder.frame = CGRectMake(0, self.frame.size.height - 1, self.frame.size.width, 1);
bottomBorder.backgroundColor = [UIColor blackColor].CGColor;
[myTextField.layer addSublayer:bottomBorder];

在Swift中:

let topBorder = CALayer()
topBorder.frame = CGRectMake(0, 0, bounds.size.width, 1)
topBorder.backgroundColor = UIColor.grayColor()
textField.layer.addSublayer(topBorder)

let bottomBorder = CALayer()
bottomBorder.frame = CGRectMake(0, bounds.size.height-1, bounds.size.width, 1)
bottomBorder.backgroundColor = UIColor.grayColor()
textField.layer.addSublayer(bottomBorder)
vmjh9lq9

vmjh9lq92#

感谢@El Captain的宝贵评论和@Bjorn Ro的回答,即使我认为它是在Objective-c中。我对这个问题的回答是(我使用的是swift 2 Xcode 7)
重写swift文件中的函数viewDidLayoutSubviews()。其代码为

override func viewDidLayoutSubviews() {
// Creates the bottom border
        let borderBottom = CALayer()
        let borderWidth = CGFloat(2.0)
        borderBottom.borderColor = UIColor.grayColor().CGColor
        borderBottom.frame = CGRect(x: 0, y: forgotPasswordEmailText.frame.height - 1.0, width: forgotPasswordEmailText.frame.width , height: forgotPasswordEmailText.frame.height - 1.0)
        borderBottom.borderWidth = borderWidth
        forgotPasswordEmailText.layer.addSublayer(borderBottom)
        forgotPasswordEmailText.layer.masksToBounds = true

// Creates the Top border
        let borderTop = CALayer()
        borderTop.borderColor = UIColor.grayColor().CGColor
        borderTop.frame = CGRect(x: 0, y: 0, width: forgotPasswordEmailText.frame.width, height: 1)
        borderTop.borderWidth = borderWidth
        forgotPasswordEmailText.layer.addSublayer(borderTop)
        forgotPasswordEmailText.layer.masksToBounds = true

    }

forgotPasswordEmailText是用于输入电子邮件的文本字段
最终输出如下所示...带有灰色彩色边框(iPhone 4s模拟器的屏幕截图)x1c 0d1x

qfe3c7zg

qfe3c7zg3#

到目前为止,对程序解决方案的好建议已经发布。但是我想我应该分享一个InterfaceBuilder解决方案...
1)在视图控制器中创建视图集合

@IBOutlet private var borderViews: [UIView]?

2)在界面构建器中创建2个UIView,高度为1px,限制在文本字段周围您希望的位置
3)将界面构建器中的2个视图连接到borderViews IBOutlet
4)通过使用setValue forKeyPath自定义两个视图的外观...例如,成功时,您可能希望边框变为绿色

setValue(UIColor.green, forKeyPath: "borderViews.backgroundColor")
yptwkmov

yptwkmov4#

在Swift 3中使用扩展名:
1.创建Swift文件

import UIKit

extension UITextField {

    func setBottomBorder() {
        self.borderStyle = .none
        self.layer.backgroundColor = UIColor.white.cgColor
        self.layer.masksToBounds = false
        self.layer.shadowColor = UIColor.gray.cgColor
        self.layer.shadowOffset = CGSize(width: 0.0, height: 1.0)
        self.layer.shadowOpacity = 1.0
        self.layer.shadowRadius = 0.0
    }
}

1.随时随地拨打电话:

PasswordField.setBottomBorder();
093gszye

093gszye5#

下面是一个很好的Swift 4实现,它也可以处理视图大小的调整:)

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    viewToShadow.backgroundColor = .white
    viewToShadow.layer.masksToBounds = false

    viewToShadow.layer.sublayers?
        .filter { layer -> Bool in
            return layer.backgroundColor == UIColor.almostBlack.alpha(0.5).cgColor
        }
        .forEach { layer in
            layer.removeFromSuperlayer()
    }

    [CGRect(x: 0.0, y: 0.0, width: viewToShadow.bounds.width, height: 0.5),
     CGRect(x: 0.0, y: viewToShadow.bounds.height, width: viewToShadow.bounds.width, height: 0.5)]
        .forEach { frame in
            let layer = CALayer()
            layer.frame = frame
            layer.backgroundColor = UIColor.almostBlack.alpha(0.5).cgColor
            viewToShadow.layer.addSublayer(layer)
    }
}
gj3fmq9x

gj3fmq9x6#

使用方便的扩展名

extension UITextField {
    func addTopBorder(){
        let bottomLine = CALayer()
        bottomLine.frame = CGRect.init(x: 0, y: 0, width: self.frame.size.width, height: 1)
        bottomLine.backgroundColor = UIColor.white.cgColor
        self.borderStyle = UITextField.BorderStyle.none
        self.layer.addSublayer(bottomLine)

    }

    func addBottomBorder(){
        let bottomLine = CALayer()
        bottomLine.frame = CGRect.init(x: 0, y: self.frame.size.height - 1, width: self.frame.size.width, height: 1)
        bottomLine.backgroundColor = UIColor.white.cgColor

        self.attributedPlaceholder = NSAttributedString(string: self.placeholder ?? "-", attributes: [NSAttributedString.Key.foregroundColor : #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)])
        self.borderStyle = UITextField.BorderStyle.none
        self.layer.addSublayer(bottomLine)

    }
}

在你的控制器中使用它

yourTextfield.addTopBorder()
yourTextfield.addBottomBorder()

别忘了在主线程上使用它

DispatchQueue.main.async {
            self.yourTextfield.addTopBorder()
           self.yourTextfield.addBottomBorder()
        }

相关问题