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
Create a simple view class with @IBInspectable properties.
...
//
// 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
}
}
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.
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.
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];
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)
}
}
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.