当前位置: 首页 > 面试题库 >

添加底线以在SwiftUI / Swift / Objective-C / Xamarin中查看

令狐增
2023-03-14
问题内容

我只想将边框保留在底部UITextField。但是我不知道如何才能将其保留在最底层

你能告诉我吗?


问题答案:

我正在创建自定义textField以使其成为SwiftUI的可重用组件

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

您可以创建一个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;


 类似资料:
  • 问题内容: 在Objective-C中,有一个称为的类方法,该方法在首次加载类时被调用。Swift中的等效项是什么? 问题答案: 在Swift 1.2之前: 编辑: 从Swift 1.2开始,您将无法再覆盖该方法。相反,查看该方法,它的行为与加载不同,它是在类第一次在某处而不是在应用程序初始加载时被调用的

  • 问题内容: 我正在将我的项目从Objective-c转换为Swift,并且正在使用一个Swift类,我正在尝试在Objective- c类中进行访问。我的问题是,在Objective-C类中不可访问。这是我的速成班: 当我查看文件时,看不到: 当我尝试在我的Objective-c类中使用时,出现编译器错误。 有人知道这个问题吗?如何在Objective-c中访问Swift协议? 问题答案: 您需要

  • 本文向大家介绍在Swift中使用Objective-C编写类、继承Objective-C类,包括了在Swift中使用Objective-C编写类、继承Objective-C类的使用技巧和注意事项,需要的朋友参考一下 互用性(互操作性)使开发者可以定义融合了 Objective-C 语言特性的Swift类。编写 Swift 类时,不仅可以继承 Objective-C 语言编写的父类,采用 Objec

  • 本文向大家介绍Objective-C语言添加元素,包括了Objective-C语言添加元素的使用技巧和注意事项,需要的朋友参考一下 示例            

  • 我尝试将名为的Swift协议导入到名为的Objective-C类中。 我从一个现有的Objective-C项目开始(我没有用xCode创建一个新的Swift项目,也没有发现如何在Xcode6中将我的Objective-C项目配置为一个Swift项目)。 在Apple文档中,他们说我必须在我的文件中包含名为的文件。但我不需要将其包含在文件中,而是包含在中。这会有问题吗? 当我尝试编译时,出现以下错误

  • 假设我在Swift中定义了一个泛型类,类似于以下内容: (我知道数组有一个稍微好一点的实现,这只是一个例子。) 在Swift中,我现在可以非常容易地使用这个类: 在Xcode 7中,我们现在在Objective-C中拥有泛型,所以我假设我可以在Objective-C中使用这个类: 然而,MyArray从未添加到我的<code>项目Swift中。h文件。即使我将其更改为从NSObject继承,它仍然