Is there a way to make gradient background color in the interface builder?

前端 未结 9 1147
终归单人心
终归单人心 2021-01-30 13:08

For my application I\'m using a TableView and using customized UITableViewCells.

I customized my cells via interface builder, not programmatically. Is there a way to als

相关标签:
9条回答
  • 2021-01-30 13:32

    Create a simple view class with @IBInspectable properties.

    1. Create gradient layer once
    2. Reuse gradient layer each layout subviews

    ...

    //
    //  GradientView.swift
    //
    //  Created by Maksim Vialykh on 23.08.2018.
    //  Copyright © 2018 Vialyx. All rights reserved.
    //
    
    import UIKit
    
    class GradientView: UIView {
    
        @IBInspectable
        var startColor: UIColor = .white
    
        @IBInspectable
        var endColor: UIColor = .black
    
        private let gradientLayerName = "Gradient"
    
        override func layoutSubviews() {
            super.layoutSubviews()
    
            setupGradient()
        }
    
        private func setupGradient() {
            var gradient: CAGradientLayer? = layer.sublayers?.first { $0.name == gradientLayerName } as? CAGradientLayer
            if gradient == nil {
                gradient = CAGradientLayer()
                gradient?.name = gradientLayerName
                layer.addSublayer(gradient!)
            }
            gradient?.frame = bounds
            gradient?.colors = [startColor.cgColor, endColor.cgColor]
            gradient?.zPosition = -1
        }
    
    }
    
    0 讨论(0)
  • 2021-01-30 13:32

    No, you can't. You could use UISegmentedControl with one segment in older sdk and xCode versions: http://chris-software.com/index.php/2009/05/13/creating-a-nice-glass-buttons/ But now you can't make less than two segments in one UISegmentedControl. And even this way you couldn't change the default colors of the buttons without coding.

    0 讨论(0)
  • 2021-01-30 13:33

    I don't know if there is a gradient option, but you could add an UIImageView to the custom cell and add an image with a gradient.

    0 讨论(0)
  • 2021-01-30 13:41
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = yourView.bounds;
    gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor blackColor] CGColor], (id)    [[UIColor whiteColor] CGColor], nil];
    [yourView.layer insertSublayer:gradient atIndex:0];
    
    0 讨论(0)
  • 2021-01-30 13:43

    This works for Swift 3.0: (Updated for Swift 4.0)

    @IBDesignable
    final class GradientView: UIView {
        @IBInspectable var startColor: UIColor = UIColor.clear
        @IBInspectable var endColor: UIColor = UIColor.clear
    
        override func draw(_ rect: CGRect) {
            let gradient: CAGradientLayer = CAGradientLayer()
            gradient.frame = CGRect(x: CGFloat(0),
                                    y: CGFloat(0),
                                    width: superview!.frame.size.width,
                                    height: superview!.frame.size.height)
            gradient.colors = [startColor.cgColor, endColor.cgColor]
            gradient.zPosition = -1
            layer.addSublayer(gradient)
        }
    }
    

    0 讨论(0)
  • 2021-01-30 13:46

    Yes this is possible : Make a image in gradient with 1 X Height pix. Set this to backgroundColor for cell.

    cell.backgroundColor =  [UIColor colorWithPatternImage:[UIImage imageNamed:@"gradImage.png"]];
    

    **You can set gradient color with code but its time taken process. If you fill better then search for that.

    0 讨论(0)
提交回复
热议问题