我只想将边框保留在底部UITextField。但是我不知道如何才能将其保留在最底层。
UITextField
你能告诉我吗?
我正在创建自定义textField以使其成为SwiftUI的可重用组件
textField
SwiftUI
struct CustomTextField: View { var placeHolder: String @Binding var value: String var lineColor: Color var width: CGFloat var body: some View { VStack { TextField(self.placeHolder, text: $value) .padding() .font(.title) Rectangle().frame(height: self.width) .padding(.horizontal, 20).foregroundColor(self.lineColor) } } }
用法:
@Binding var userName: String @Binding var password: String var body: some View { VStack(alignment: .center) { CustomTextField(placeHolder: "Username", value: $userName, lineColor: .white, width: 2) CustomTextField(placeHolder: "Password", value: $password, lineColor: .white, width: 2) } }
斯威夫特5.0
我在这里使用视觉格式语言(VFL),这将允许在any中添加一行UIControl。
UIControl
您可以创建一个UIView扩展类,例如UIView+Extention.swift
UIView
UIView+Extention.swift
import UIKit enum LINE_POSITION { case LINE_POSITION_TOP case LINE_POSITION_BOTTOM } extension UIView { func addLine(position : LINE_POSITION, color: UIColor, width: Double) { let lineView = UIView() lineView.backgroundColor = color lineView.translatesAutoresizingMaskIntoConstraints = false // This is important! self.addSubview(lineView) let metrics = ["width" : NSNumber(value: width)] let views = ["lineView" : lineView] self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[lineView]|", options:NSLayoutConstraint.FormatOptions(rawValue: 0), metrics:metrics, views:views)) switch position { case .LINE_POSITION_TOP: self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[lineView(width)]", options:NSLayoutConstraint.FormatOptions(rawValue: 0), metrics:metrics, views:views)) break case .LINE_POSITION_BOTTOM: self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[lineView(width)]|", options:NSLayoutConstraint.FormatOptions(rawValue: 0), metrics:metrics, views:views)) break } } }
textField.addLine(position: .LINE_POSITION_BOTTOM, color: .darkGray, width: 0.5)
目标C:
您可以将此帮助程序方法添加到全局帮助程序类(我使用了全局类方法)或同一视图控制器中(使用实例方法)。
typedef enum : NSUInteger { LINE_POSITION_TOP, LINE_POSITION_BOTTOM } LINE_POSITION; - (void) addLine:(UIView *)view atPosition:(LINE_POSITION)position withColor:(UIColor *)color lineWitdh:(CGFloat)width { // Add line UIView *lineView = [[UIView alloc] init]; [lineView setBackgroundColor:color]; [lineView setTranslatesAutoresizingMaskIntoConstraints:NO]; [view addSubview:lineView]; NSDictionary *metrics = @{@"width" : [NSNumber numberWithFloat:width]}; NSDictionary *views = @{@"lineView" : lineView}; [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[lineView]|" options: 0 metrics:metrics views:views]]; switch (position) { case LINE_POSITION_TOP: [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[lineView(width)]" options: 0 metrics:metrics views:views]]; break; case LINE_POSITION_BOTTOM: [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[lineView(width)]|" options: 0 metrics:metrics views:views]]; break; default: break; } }
[self addLine:self.textField atPosition:LINE_POSITION_TOP withColor:[UIColor darkGrayColor] lineWitdh:0.5];
Xamarin代码:
var border = new CALayer(); nfloat width = 2; border.BorderColor = UIColor.Black.CGColor; border.Frame = new CoreGraphics.CGRect(0, textField.Frame.Size.Height - width, textField.Frame.Size.Width, textField.Frame.Size.Height); border.BorderWidth = width; textField.Layer.AddSublayer(border); textField.Layer.MasksToBounds = true;