简体   繁体   English

如何使两个插座都扩展(SWIFT 2)

[英]How to make extension both outlet (swift 2)

I created extension for UITextField. 我为UITextField创建了扩展名。 And I need the same extension for UITextView. 而且我需要UITextView的相同扩展名。 How to make this extension available for all other views? 如何使此扩展程序可用于所有其他视图?

My extension code: 我的扩展代码:

extension UITextField {

    func addTopBorderWithColor(color: UIColor, height: CGFloat) {
        let border = CALayer()
        border.backgroundColor = color.CGColor
        border.frame = CGRectMake(0, 0, self.frame.size.width, height)
        self.layer.addSublayer(border)
    }

    func addRightBorderWithColor(color: UIColor, height: CGFloat) {
        let border = CALayer()
        border.backgroundColor = color.CGColor
        border.frame = CGRectMake(self.frame.size.width - height, 0, height, self.frame.size.height)
        self.layer.addSublayer(border)
    }

    func addBottomBorderWithColor(color: UIColor, height: CGFloat) {
        let border = CALayer()
        border.backgroundColor = color.CGColor
        border.frame = CGRectMake(0, self.frame.size.height - height, self.frame.size.width, height)
        self.layer.addSublayer(border)
    }

    func addLeftBorderWithColor(color: UIColor, height: CGFloat) {
        let border = CALayer()
        border.backgroundColor = color.CGColor
        border.frame = CGRectMake(0, 0, height, self.frame.size.height)
        self.layer.addSublayer(border)
    }
}

You should only create the extension for UIView . 您应该只为UIView创建扩展。 As the other classes ( UITextField , UITextView , UILabel , ...) extend UIView , they all should have your functions available. 随着其他类( UITextFieldUITextViewUILabel ,...)扩展UIView ,它们都应具有可用的功能。

NOTE : This requires that the functions work for UIView and don't contain specific operations (eg accessing properties only available in UITextView ). 注意 :这要求这些函数适用于UIView,并且不包含特定的操作(例如,访问仅在UITextView可用的属性)。

In the case when you want an extension for only a few classes, you could define a protocol and provide default implementations in the protocol, then extend the classes with the new protocol. 如果只想扩展几个类,则可以定义一个协议并在该协议中提供默认实现,然后使用新协议扩展这些类。 Here's a trivial example you can run in playground: 这是一个可以在操场上运行的简单示例:

protocol foo {
    func bar() -> String;
}

extension foo {
    func bar() -> String {
        return "bar"
    }
}

extension Float: foo {}
extension Int: foo {}


let i = 12
print(i.bar())

let f:Float = 1.0
print (f.bar())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift:不小心将插座添加到两个不同的文件中,删除了两个插座 - Swift: accidentally added an outlet to two different files, deleted both outlets 尝试使用选项卡栏项目进行操作时很快,我只能创建一个插座或插座集合,该如何操作? - swift when I try to make an action with my tab bar item, I can only make an outlet or an outlet collection how can I make an action? 如何为多个类Swift进行扩展 - How to make extension for multiple classes Swift 如何在 Swift 中为 Dictionary 制作自定义扩展? - How can make a custom extension for Dictionary in Swift? 如何在 Swift 5 中为多种类型进行扩展 - How to make an extension for multiple types in Swift 5 如何在 Swift 中进行通用扩展? - How can I make a generic extension in Swift? 如何在 Swift 中对 Collection 泛型进行约束扩展? - How to make a constrained extension of Collection generic in Swift? 如何使用swift在集合视图中制作页眉和页脚 - How to make both header and footer in collection view with swift Swift-如何使协议既是类又需要采用某种类型 - Swift - how to make protocol to be both class and need to adopt certain type 如何为Objective-C和Swift创建通用iOS库? - How to make an universal iOS library for both Objective-C and Swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM