在UITextField的开头创建空间


149

我想在UITextField的开头保留一些空间,就像这里: 向UITextField添加左侧边距

但是我不知道如何用Swift做到这一点。


好吧,您不能在Objective-C中子类化swift对象,但是您可以用另一种方法来实现...所以我猜您只是调整了答案并将其与以下代码结合使用:developer.apple.com/library/prerelease/ ios / documentation / Swift /…
Grady Player

1
这可能不是最好的解决方案,但是您可以制作一个uiview * paddingView并执行UITextField.leftView = paddingView。因此,使填充视图具有所需的宽度。
ipalibowhyte 2014年

1
填充视图将只是具有您想要的宽度的普通UIView
Grady Player

对于Swift 5:textField.layoutMargins.left = 20
Oleksandr

Answers:


283

这就是我现在正在使用的:

斯威夫特4.2

class TextField: UITextField {

    let padding = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)

    override open func textRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.inset(by: padding)
    }

    override open func placeholderRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.inset(by: padding)
    }

    override open func editingRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.inset(by: padding)
    }
}

斯威夫特4

class TextField: UITextField {

    let padding = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)

    override open func textRect(forBounds bounds: CGRect) -> CGRect {
        return UIEdgeInsetsInsetRect(bounds, padding)
    }

    override open func placeholderRect(forBounds bounds: CGRect) -> CGRect {
        return UIEdgeInsetsInsetRect(bounds, padding)
    }

    override open func editingRect(forBounds bounds: CGRect) -> CGRect {
        return UIEdgeInsetsInsetRect(bounds, padding)
    }
}

斯威夫特3:

class TextField: UITextField {

    let padding = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)

    override func textRect(forBounds bounds: CGRect) -> CGRect {
        return UIEdgeInsetsInsetRect(bounds, padding)
    }

    override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
        return UIEdgeInsetsInsetRect(bounds, padding)
    }

    override func editingRect(forBounds bounds: CGRect) -> CGRect {
        return UIEdgeInsetsInsetRect(bounds, padding)
    }
}

我从未设置其他填充,但是您可以进行调整。此类不会处理textfield上的rightView和leftView。如果您希望它得到正确处理,则可以使用类似(objc中的示例,我只需要rightView即可:

- (CGRect)textRectForBounds:(CGRect)bounds {
    CGRect paddedRect = UIEdgeInsetsInsetRect(bounds, self.insets);

    if (self.rightViewMode == UITextFieldViewModeAlways || self.rightViewMode == UITextFieldViewModeUnlessEditing) {
        return [self adjustRectWithWidthRightView:paddedRect];
    }
    return paddedRect;
}

- (CGRect)placeholderRectForBounds:(CGRect)bounds {
    CGRect paddedRect = UIEdgeInsetsInsetRect(bounds, self.insets);

    if (self.rightViewMode == UITextFieldViewModeAlways || self.rightViewMode == UITextFieldViewModeUnlessEditing) {
        return [self adjustRectWithWidthRightView:paddedRect];
    }
    return paddedRect;
}

- (CGRect)editingRectForBounds:(CGRect)bounds {
    CGRect paddedRect = UIEdgeInsetsInsetRect(bounds, self.insets);

    if (self.rightViewMode == UITextFieldViewModeAlways || self.rightViewMode == UITextFieldViewModeWhileEditing) {
        return [self adjustRectWithWidthRightView:paddedRect];
    }
    return paddedRect;
}

- (CGRect)adjustRectWithWidthRightView:(CGRect)bounds {
    CGRect paddedRect = bounds;
    paddedRect.size.width -= CGRectGetWidth(self.rightView.frame);

    return paddedRect;
}

为什么在计算宽度和高度时将顶部和左侧插图加倍?不需要这样做。您应该将两个相关的插图加在一起,然后从原始范围中减去总数。或者只是依次减去两者。
2015年

1
@ Mr.UB检查当前设备是哪个平台,并基于该平台创建不同的填充。stackoverflow.com/questions/4567728/…。大概是这样的
Haagenti

Apple提供与该newBounds方法等效的UIEdgeInsetsInsetRect功能。相反,return self.newBounds(bounds)您可以使用return UIEdgeInsetsInsetRect(bounds, padding)和删除该newBounds方法。
Mobile Dan

如果您的文本字段是多行,则使占位符文本居中并取代textAlignment = .left和contentVerticalAlignment =
.top

@Ryan已经有一段时间了,但是UITextField仅是我认为的一行。然后应将UITextView用于multiline。
Haagenti

194

如果使用扩展,则无需子类化UITextField,新功能将可用于您应用程序中的任何UITextField:

extension UITextField {
    func setLeftPaddingPoints(_ amount:CGFloat){
        let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: amount, height: self.frame.size.height))
        self.leftView = paddingView
        self.leftViewMode = .always
    }
    func setRightPaddingPoints(_ amount:CGFloat) {
        let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: amount, height: self.frame.size.height))
        self.rightView = paddingView
        self.rightViewMode = .always
    }
}

当我需要在应用程序中的任何地方设置文本字段的填充时,只需执行以下操作:

    textField.setLeftPaddingPoints(10)
    textField.setRightPaddingPoints(10)

使用Swift扩展,无需子类即可直接将功能添加到UITextField中。

希望这可以帮助!


8
优秀的解决方案,非常优雅。我所做的唯一更改是将它们添加到一个函数中,因此得到了诸如textField.setPaddingFor(left:10,right:10)之类的内容。这两个参数都是可选的,因此,如果您通过nil,则填充将为
0。– Nermin Sehic

4
大!但是,如果您设置textField.clearButtonMode = .always,则仅需设置左填充。右侧填充将清除按钮移到右侧。
彼得·克林茨

2
一种观察。它更像是前导/尾随填充。但是,奇怪的是它响应文本字段的文本对齐!不是应用程序语言的方向。
哈桑

如何在UILabel中设置?
无辜的

大!适用于主要文本以及占位符。
Akash Bhardwaj

70

X,Y,Z是您想要的值

textField.layer.sublayerTransform = CATransform3DMakeTranslation(x, y, z)

10
这似乎不适用于textField.clearButtonMode = UITextFieldViewMode.Always –清除按钮也向右移动
CaptainProton

1
当需要显示“清除”按钮时不起作用...清除按钮也已移动。
xdev

这个响应很短,但是还不完整,可能会在以后出问题。@Adrian你有一个很好的观点,但事实并非如此。之所以要使用子类,是因为所有的极端情况。此代码可能在子类解决方案之前崩溃。但是,你是对的,你不应该写的代码并不是必需的,并且可以通过使用给定库提供,但你不应该滥用标准库或者
哈艮地

太酷了!谢谢!
Booharin

46

可以通过将leftView/ 设置rightView为来获得这种余量UITextField

为Swift 4更新

// Create a padding view for padding on left
textField.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 15, height: textField.frame.height))
textField.leftViewMode = .always

// Create a padding view for padding on right
textField.rightView = UIView(frame: CGRect(x: 0, y: 0, width: 15, height: textField.frame.height))
textField.rightViewMode = .always

我只是UIView在文本字段的左侧和右侧添加/放置了一个。因此,现在键入将在视图之后开始。

谢谢

希望这可以帮助...


1
如果在“目标c”中需要代码,则UIView * paddingView = [[UIView alloc] initWithFrame:CGRectMake(0,0,15,self。userNameTxtFldOutlet.frame.size.height)];自。userNameTxtFldOutlet.leftView = paddingView; 自。userNameTxtFldOutlet.leftViewMode = UITextFieldViewModeAlways;
阿瓦恩

1
该解决方案比上面提到的子类更为简洁。应尽可能避免子类化。我建议以下阅读krakendev.io/blog/subclassing-can-suck-and-heres-why
西尔

33

斯威夫特4,Xcode 9

我喜欢Pheepster的答案,但是我们如何从扩展中完成所有工作,而无需VC代码或任何子类:

import UIKit

@IBDesignable
extension UITextField {

    @IBInspectable var paddingLeftCustom: CGFloat {
        get {
            return leftView!.frame.size.width
        }
        set {
            let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: newValue, height: frame.size.height))
            leftView = paddingView
            leftViewMode = .always
        }
    }

    @IBInspectable var paddingRightCustom: CGFloat {
        get {
            return rightView!.frame.size.width
        }
        set {
            let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: newValue, height: frame.size.height))
            rightView = paddingView
            rightViewMode = .always     
        }
    }
}

这样做会更安全rightView?.frame.size.width ?? 0
Tal

它可能。对于我自己,我永远不要打电话给吸气剂,这样就不会打扰我。
Teodor Ciuraru

1
伙计们,我也修改了方法的名称,从paddingLeftto paddingLeftCustom到另一个。如果我还没有这样做,那么当您使用具有UITextView(例如UISearchBar)的View时,将出现一个跟随我两个星期的错误。只是...不要设置默认名称。
Teodor Ciuraru

17

Swift 4.2和Xcode 10中

最初,我的文本字段是这样的。

在此处输入图片说明

在左侧添加填充后,我的文本字段是...

在此处输入图片说明

//Code for left padding 
textFieldName.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: textFieldName.frame.height))
textFieldName.leftViewMode = .always

这样我们也可以创建右侧。(textFieldName.rightViewMode = .always)

如果要使用SharedInstance类型代码(每次使用一次写一次即可),请参见以下代码。

//This is my shared class
import UIKit
class SharedClass: NSObject {
    static let sharedInstance = SharedClass()

    //This is my padding function.
    func textFieldLeftPadding(textFieldName: UITextField) {
    // Create a padding view
    textFieldName.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 3, height: textFieldName.frame.height))
    textFieldName.leftViewMode = .always//For left side padding
    textFieldName.rightViewMode = .always//For right side padding
    }

    private override init() {

    }
}

现在像这样调用此函数。

//This single line is enough
SharedClass.sharedInstance.textFieldLeftPadding(textFieldName:yourTF)

2
扩展不应该比引入共享类来完成这样的次要任务更好吗?
Sharkes Monken

@ Sharkes Monken,我不明白
iOS

@ Sharkes Monken,您能为我解释一下吗?谢谢。
iOS

1
我认为这意味着该函数的扩展UITextField,此辅助功能的单例效果不好
logan.Nguyen

14

简单的Swift 3解决方案-向viewDidLoad添加代码:

let indentView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 20))
textField.leftView = indentView
textField.leftViewMode = .always

不需要可笑的长代码


这不适用于UISearchBar中的UITextField。:(我需要在这种情况下特别有效的解决方案:(
Miki,

@livtay当您使用clearButtonMode或想要有一个leftView等时,这将不起作用。尽管这是一个快速的胜利,但是只要知道您要进入的洞
即可。– Haagenti

13

使用经过测试的我的Swift 5扩展程序:

extension UITextField {

enum PaddingSpace {
    case left(CGFloat)
    case right(CGFloat)
    case equalSpacing(CGFloat)
}

func addPadding(padding: PaddingSpace) {

    self.leftViewMode = .always
    self.layer.masksToBounds = true

    switch padding {

    case .left(let spacing):
        let leftPaddingView = UIView(frame: CGRect(x: 0, y: 0, width: spacing, height: self.frame.height))
        self.leftView = leftPaddingView
        self.leftViewMode = .always

    case .right(let spacing):
        let rightPaddingView = UIView(frame: CGRect(x: 0, y: 0, width: spacing, height: self.frame.height))
        self.rightView = rightPaddingView
        self.rightViewMode = .always

    case .equalSpacing(let spacing):
        let equalPaddingView = UIView(frame: CGRect(x: 0, y: 0, width: spacing, height: self.frame.height))
        // left
        self.leftView = equalPaddingView
        self.leftViewMode = .always
        // right
        self.rightView = equalPaddingView
        self.rightViewMode = .always
    }
}
}

如何使用

// equal padding
yourTextField.addPadding(padding: .equalSpacing(10)) 

// padding right 
yourTextField.addPadding(padding: .right(10))

// padding left
yourTextField.addPadding(padding: .left(10)) 

@JoséRaúlToledanoRTHX :)
法比奥

优雅。谢谢。
卡洛

@Carlo Grazie mille Carlo :)
Fabio

10

在Swift 5中为UITextField创建填充视图

func txtPaddingVw(txt:UITextField) {
    let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 5, height: 5))
    txt.leftViewMode = .always
    txt.leftView = paddingView
}

8

继承UITextField是方法。打开一个游乐场并添加以下代码:

class MyTextField : UITextField {
    var leftTextMargin : CGFloat = 0.0

    override func textRectForBounds(bounds: CGRect) -> CGRect {
        var newBounds = bounds
        newBounds.origin.x += leftTextMargin
        return newBounds
    }

    override func editingRectForBounds(bounds: CGRect) -> CGRect {
        var newBounds = bounds
        newBounds.origin.x += leftTextMargin
        return newBounds
    }
}

let tf = MyTextField(frame: CGRect(x: 0, y: 0, width: 100, height: 44))
tf.text = "HELLO"
tf.leftTextMargin = 25
tf.setNeedsLayout()
tf.layoutIfNeeded()

这几乎是完美的。您可能有一个占位符,该占位符具有类似的明智方法:“ placeholderRectForBounds”也应覆盖该占位符,并且从宽度中减去添加为x的内容,否则当文本超出长度时,您将看不到哪种类型领域
Haagenti 2014年

如果左边为25,则宽度应为负50,以确保填充相等
Haagenti 2014年

7

这是Haagenti的答案已更新为Swift 4.2:

class PaddedTextField: UITextField {

    func getPadding(plusExtraFor clearButtonMode: ViewMode) -> UIEdgeInsets {
        var padding = UIEdgeInsets(top: 11, left: 16, bottom: 11, right: 16)

        // Add additional padding on the right side when showing the clear button
        if self.clearButtonMode == .always || self.clearButtonMode == clearButtonMode {
            padding.right = 28
        }

        return padding
    }

    override open func textRect(forBounds bounds: CGRect) -> CGRect {
        let padding = getPadding(plusExtraFor: .unlessEditing)
        return bounds.inset(by: padding)
    }

    override open func placeholderRect(forBounds bounds: CGRect) -> CGRect {
        let padding = getPadding(plusExtraFor: .unlessEditing)
        return bounds.inset(by: padding)
    }

    override open func editingRect(forBounds bounds: CGRect) -> CGRect {
        let padding = getPadding(plusExtraFor: .whileEditing)
        return bounds.inset(by: padding)
    }

}

参考:升级到Swift 4.2

编辑:帐户清除按钮。


6

使用所需的填充空间创建UIView并将其添加到textfield.leftView成员,并将textfield.leftViewMode成员设置为UITextFieldViewMode.Always

// For example if you have textfield named title
@IBOutlet weak var title: UITextField!
// Create UIView 
let paddingView : UIView = UIView(frame: CGRectMake(0, 0, 5, 20))
//Change your required space instaed of 5.
title.leftView = paddingView
title.leftViewMode = UITextFieldViewMode.Always

5

将此代码放入您的viewDidLoad()

textField.delegate = self

let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: self.textField.frame.height))
textField.leftView = paddingView
textField.leftViewMode = UITextFieldViewMode.always

这个对我有用 :)


5

这段代码为我节省了:

对于Xamarin.iOS:

textField.Layer.SublayerTransform = CATransform3D.MakeTranslation(5, 0, 0);

对于Swift:

textField.layer.sublayerTransform = CATransform3DMakeTranslation(5, 0, 0);

4

ScareCrow在Swift 3中的答案

let padding = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5);

override func textRect(forBounds bounds: CGRect) -> CGRect {
    return UIEdgeInsetsInsetRect(bounds, padding)
}

override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
    return UIEdgeInsetsInsetRect(bounds, padding)
}

override func editingRect(forBounds bounds: CGRect) -> CGRect {
    return UIEdgeInsetsInsetRect(bounds, padding)
}

4

在Swift 3中,您可以将自定义UITextField与在其构造函数中设置的缩进一起使用。在控制器中不需要额外的声明。

class CustomTextField : UITextField {

private let indentView = UIView(frame: CGRect(x: 0, y:0, width: 10, height: 10))

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    self.leftView = indentView
    self.leftViewMode = .always 
        }
}

4

简单的方法:通过扩展UITextField来做到这一点

extension UITextField {

   func setPadding(left: CGFloat? = nil, right: CGFloat? = nil){
       if let left = left {
          let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: left, height: self.frame.size.height))
          self.leftView = paddingView
          self.leftViewMode = .always
       }

       if let right = right {
           let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: right, height: self.frame.size.height))
           self.rightView = paddingView
           self.rightViewMode = .always
       }
   }

}

然后,您可以通过以下方式将padding设置为任意边缘:

textField.setPadding(left: 5, right: 5)

尝试使用相同的代码,但是在iOS 13上使用彩色的-left-和-right-视图,并使用xCode 11 ....)来构建它)边缘,因此添加的视图不完全可见
Massmaker '19

4

我更喜欢使用IBDesignable类和IBInspectable属性来允许我通过Xcode故事板设置填充并使其可重复使用。我还更新了代码以在Swift 4中工作。

import Foundation
import UIKit

@IBDesignable
class PaddableTextField: UITextField {

    var padding = UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0)

    @IBInspectable var left: CGFloat = 0 {
        didSet {
            adjustPadding()
        }
    }

    @IBInspectable var right: CGFloat = 0 {
        didSet {
            adjustPadding()
        }
    }

    @IBInspectable var top: CGFloat = 0 {
        didSet {
            adjustPadding()
        }
    }

    @IBInspectable var bottom: CGFloat = 0 {
        didSet {
            adjustPadding()
        }
    }

    func adjustPadding() {
         padding = UIEdgeInsets(top: top, left: left, bottom: bottom, right: right)

    }

    override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
    }

    override func textRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.inset(by: UIEdgeInsets(top: top, left: left, bottom: bottom, right: right))
    }

    override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.inset(by: UIEdgeInsets(top: top, left: left, bottom: bottom, right: right))
    }

    override func editingRect(forBounds bounds: CGRect) -> CGRect {
         return bounds.inset(by: UIEdgeInsets(top: top, left: left, bottom: bottom, right: right))
    }
}

2

*在Swift 5中扩展UITextField *

import UIKit

@IBDesignable
extension UITextField {

    @IBInspectable var paddingLeftCustom: CGFloat {
        get {
            return leftView!.frame.size.width
        }
        set {
            let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: newValue, height: frame.size.height))
            leftView = paddingView
            leftViewMode = .always
        }
    }

    @IBInspectable var paddingRightCustom: CGFloat {
        get {
            return rightView!.frame.size.width
        }
        set {
            let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: newValue, height: frame.size.height))
            rightView = paddingView
            rightViewMode = .always
        }
    }

}

0
//MARK:-  Use this class for different type of Roles

import UIKit

class HelperExtensionViewController: UIViewController {

}

//MARK:- Extension

extension UIImageView
{
    func setImageCornerRadius()
    {
        self.layer.cornerRadius = self.frame.size.height/2
        self.clipsToBounds = true
    }

    func setImageCornerRadiusInPoints(getValue:CGFloat)
    {
        self.layer.cornerRadius = getValue
        self.clipsToBounds = true
    }
}

extension UIButton
{
    func setButtonCornerRadiusOnly()
    {
        self.layer.cornerRadius = self.frame.size.height/2
        self.clipsToBounds = true
    }

    func setBtnCornerRadiusInPoints(getValue:CGFloat)
    {
        self.layer.cornerRadius = getValue
        self.clipsToBounds = true
    }


}

extension UITextField
{
    func setTextFieldCornerRadiusWithBorder()
    {
        self.layer.cornerRadius = self.frame.size.height/2
        self.layer.borderColor = UIColor.darkGray.cgColor
        self.backgroundColor = UIColor.clear
        self.layer.borderWidth = 0.5
        self.clipsToBounds = true
    }

    func setLeftPaddingPoints(_ amount:CGFloat){
        let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: amount, height: self.frame.size.height))
        self.leftView = paddingView
        self.leftViewMode = .always
    }
    func setRightPaddingPoints(_ amount:CGFloat) {
        let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: amount, height: self.frame.size.height))
        self.rightView = paddingView
        self.rightViewMode = .always
    }
}



extension UIView
{

    func setCornerRadius()
    {
        self.layer.cornerRadius = self.frame.size.height/2
        self.clipsToBounds = true
    }

    // OUTPUT 1
    func setViewCornerRadiusWithBorder()
    {
        self.layer.cornerRadius = self.frame.size.height/2
        self.layer.borderColor = UIColor.init(red: 95.0/255.0, green: 229.0/255.0, blue: 206.0/255.0, alpha: 1.0).cgColor
        self.backgroundColor = UIColor.clear
        self.layer.borderWidth = 1.0
        self.clipsToBounds = true
    }

    func layoutSubviews(myView:UIView)
    {
        let shadowPath = UIBezierPath(rect: myView.bounds)
        myView.layer.masksToBounds = false
        myView.layer.shadowColor = UIColor.lightGray.cgColor
        myView.layer.shadowOffset = CGSize(width: -1.0, height: 2.0)
        myView.layer.shadowOpacity = 0.5
        myView.layer.shadowPath = shadowPath.cgPath
    }

    func layoutSubviews2(myView:UIView)
    {
        let shadowPath = UIBezierPath(rect: myView.bounds)
        myView.clipsToBounds = true
        myView.layer.masksToBounds = false
        myView.layer.shadowColor = UIColor.black.cgColor
        myView.layer.shadowOffset = CGSize(width: 0.0, height: 1.0)
        myView.layer.shadowOpacity = 0.2
        myView.layer.shadowPath = shadowPath.cgPath

    }

    func setViewCornerRadiusInPoints(getValue:CGFloat)
    {
        self.layer.cornerRadius = getValue
        self.clipsToBounds = true
    }


    func dropShadow(scale: Bool = true) {
        layer.masksToBounds = false
        layer.shadowColor = UIColor.black.cgColor
        layer.shadowOpacity = 0.5
        layer.shadowOffset = CGSize(width: -1, height: 1)
        layer.shadowRadius = 1

        layer.shadowPath = UIBezierPath(rect: bounds).cgPath
        layer.shouldRasterize = true
        layer.rasterizationScale = scale ? UIScreen.main.scale : 1
    }

    // OUTPUT 2
    func dropShadow(color: UIColor, opacity: Float = 0.5, offSet: CGSize, radius: CGFloat = 1, scale: Bool = true) {
        layer.masksToBounds = false
        layer.shadowColor = color.cgColor
        layer.shadowOpacity = opacity
        layer.shadowOffset = offSet
        layer.shadowRadius = radius

        layer.shadowPath = UIBezierPath(rect: self.bounds).cgPath
        layer.shouldRasterize = true
        layer.rasterizationScale = scale ? UIScreen.main.scale : 1
    }

    func setGradientBackground(myview:UIView) {
        let colorTop =  UIColor(red: 100.0/255.0, green: 227.0/255.0, blue: 237.0/255.0, alpha: 1.0).cgColor
        let colorBottom = UIColor(red: 141.0/255.0, green: 109.0/255.0, blue: 164.0/255.0, alpha: 1.0).cgColor

        let gradientLayer = CAGradientLayer()
        gradientLayer.colors = [colorTop, colorBottom]
        gradientLayer.locations = [1.0, 1.0]
        gradientLayer.frame = myview.bounds

        myview.layer.insertSublayer(gradientLayer, at:0)
    }
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.