Skip to content

Grading the UIView background

   

This is a sample code to make the background of UIView gradient.

Reference 【Swift x iOSアプリ開発】Viewの背景色にグラデーションを設定する方法 Swift UIViewの背景をグラデーションさせる方法 -iPhoneアプリ開発-

alt alt alt

import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//グラデーションをつける
let gradientLayer = CAGradientLayer()
gradientLayer.frame = self.view.bounds
//グラデーションさせるカラーの設定
//今回は、徐々に色を濃くしていく
let color1 = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1).cgColor //白
let color2 = UIColor(red: 120/256.0, green: 150/256.0, blue: 256/256.0, alpha: 1).cgColor //水色
//CAGradientLayerにグラデーションさせるカラーをセット
gradientLayer.colors = [color1, color2]
//グラデーションの開始地点・終了地点の設定
//上が白で下が水色
gradientLayer.startPoint = CGPoint.init(x: 0.5, y: 0)
gradientLayer.endPoint = CGPoint.init(x: 0.5 , y:1 )
//左が白で右が水色
//gradientLayer.startPoint = CGPoint.init(x: 0, y: 0.5)
//gradientLayer.endPoint = CGPoint.init(x: 1 , y:0.5)
//左上が白で右下が水色
//gradientLayer.startPoint = CGPoint.init(x: 0, y: 0)
//gradientLayer.endPoint = CGPoint.init(x: 1 , y:1)
//ViewControllerのViewレイヤーにグラデーションレイヤーを挿入する
self.view.layer.insertSublayer(gradientLayer,at:0)
}
}

  1. Implementing a custom button class
  2. Edit TableView to delete a Cell
  3. Edit TableView and delete a Cell (side-slide).
  4. Separate numbers into commas for each three digits (in Japanese yen)
  5. Get the current date and day of the week and the current time
  6. Customize the layout of TableVIew using CustumTableViewCell and accessoryView.
  7. Adding a TextField to UIAlertController