Snapchat 导航栏样式 - Swift
Snapchat Navigation Bar Style - Swift
Snapchat是如何把这个导航条做成背景渐变圆角的。谢谢!
我没有足够大的声誉来 post 图片,但它是向人们展示你的 snapchat 的视图,一直到左边。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
// Sets background to a blank/empty image
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
// Sets shadow (line below the bar) to a blank image
UINavigationBar.appearance().shadowImage = UIImage()
// Sets translucent background's color
UINavigationBar.appearance().backgroundColor = .clear
// Set translucent
UINavigationBar.appearance().isTranslucent = true
return true
}
在我的 UITableViewControllerClass 中:
override func viewDidLoad() {
super.viewDidLoad()
tableView?.layer.cornerRadius = 15
//Declare a gradient
let gradient: CAGradientLayer = CAGradientLayer()
//Set the two colors of your gradient
gradient.colors = [UIColor.purple.cgColor, UIColor.red.cgColor]
//Set it's location
gradient.locations = [0.0 , 1.0]
gradient.startPoint = CGPoint(x: 0.0, y: 0.0)
gradient.endPoint = CGPoint(x: 1.0, y: 1.0)
gradient.frame = CGRect(x: 0.0, y: 0.0, width: (self.navigationController?.navigationBar.frame.size.width)!, height: (self.navigationController?.navigationBar.frame.size.height)!)
self.navigationController?.navigationBar.layer.insertSublayer(gradient, at: 0)
}
据我了解,您希望在 table 视图中使用渐变作为背景和圆角。
这是我的 ViewController.
的代码
class ViewController: UIViewController {
@IBOutlet var backroundView: UIView!
@IBOutlet weak var viewContainingTableView: UIView!
@IBOutlet weak var topView: UIView!
@IBOutlet weak var navigationBar: UINavigationBar!
override func viewDidLoad() {
super.viewDidLoad()
//Change the corner radius to your liking
viewContainingTableView.layer.cornerRadius = 25
//Make the backround of your 'navigation controller' transparent
topView.backgroundColor = UIColor.white.withAlphaComponent(0.0)
//Declare a gradient
let gradient: CAGradientLayer = CAGradientLayer()
//Set the two colors of your gradient
gradient.colors = [UIColor.purple.cgColor, UIColor.red.cgColor]
//Set it's location
gradient.locations = [0.0 , 1.0]
gradient.startPoint = CGPoint(x: 0.0, y: 0.0)
gradient.endPoint = CGPoint(x: 1.0, y: 1.0)
gradient.frame = CGRect(x: 0.0, y: 0.0, width: self.view.frame.size.width, height: self.view.frame.size.height)
self.backroundView.layer.insertSublayer(gradient, at: 0)
//self.view.layer.insertSublayer(gradient, at: 0)
}
}
将 App Delegate 中的 didFinishLaunchingWithOptions 方法更改为以下内容:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
// Sets background to a blank/empty image
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
// Sets shadow (line below the bar) to a blank image
UINavigationBar.appearance().shadowImage = UIImage()
// Sets translucent background's color
UINavigationBar.appearance().backgroundColor = .clear
// Set translucent
UINavigationBar.appearance().isTranslucent = true
return true
}
这是我的视图层次结构:
这是它给出的内容:
要在没有情节提要的情况下执行此操作,请将 ViewController 代码更改为以下内容:
class ViewController: UIViewController {
var viewContainingTableView = UIView()
let navigationBarView = UINavigationBar()
let topView = UIView()
let tableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
let margins = view.layoutMarginsGuide
view.addSubview(topView)
topView.translatesAutoresizingMaskIntoConstraints = false
topView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
topView.heightAnchor.constraint(equalToConstant: 60).isActive = true
topView.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
topView.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
topView.addSubview(navigationBarView)
navigationBarView.translatesAutoresizingMaskIntoConstraints = false
navigationBarView.topAnchor.constraint(equalTo: topView.topAnchor).isActive = true
navigationBarView.bottomAnchor.constraint(equalTo: topView.bottomAnchor).isActive = true
navigationBarView.leadingAnchor.constraint(equalTo: topView.leadingAnchor).isActive = true
navigationBarView.trailingAnchor.constraint(equalTo: topView.trailingAnchor).isActive = true
self.navigationItem.title = "Test"
let buttonNavBar = UIBarButtonItem(title: "Button", style: .plain, target: nil, action: #selector(buttonAction))
navigationItem.rightBarButtonItem = buttonNavBar
navigationBarView.setItems([navigationItem], animated: false)
view.addSubview(viewContainingTableView)
viewContainingTableView.translatesAutoresizingMaskIntoConstraints = false
viewContainingTableView.topAnchor.constraint(equalTo: topView.bottomAnchor, constant: 0).isActive = true
viewContainingTableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
viewContainingTableView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
viewContainingTableView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
viewContainingTableView.backgroundColor = UIColor.white
viewContainingTableView.addSubview(tableView)
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.topAnchor.constraint(equalTo: viewContainingTableView.topAnchor, constant: 20).isActive = true
tableView.bottomAnchor.constraint(equalTo: viewContainingTableView.bottomAnchor).isActive = true
tableView.leadingAnchor.constraint(equalTo: viewContainingTableView.leadingAnchor).isActive = true
tableView.trailingAnchor.constraint(equalTo: viewContainingTableView.trailingAnchor).isActive = true
view.setNeedsUpdateConstraints()
view.updateConstraints()
//Change the corner radius to your liking
viewContainingTableView.layer.cornerRadius = 25
//Make the backround of your 'navigation controller' transparent
topView.backgroundColor = UIColor.white.withAlphaComponent(0.0)
//Declare a gradient
let gradient: CAGradientLayer = CAGradientLayer()
//Set the two colors of your gradient
gradient.colors = [UIColor.purple.cgColor, UIColor.red.cgColor]
//Set it's location
gradient.locations = [0.0 , 1.0]
gradient.startPoint = CGPoint(x: 0.0, y: 0.0)
gradient.endPoint = CGPoint(x: 1.0, y: 1.0)
gradient.frame = CGRect(x: 0.0, y: 0.0, width: self.view.frame.size.width, height: self.view.frame.size.height)
self.view.layer.insertSublayer(gradient, at: 0)
}
@objc func buttonAction() {
//Function of your button: add code to execute when button is pressed
}
}
Snapchat是如何把这个导航条做成背景渐变圆角的。谢谢!
我没有足够大的声誉来 post 图片,但它是向人们展示你的 snapchat 的视图,一直到左边。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
// Sets background to a blank/empty image
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
// Sets shadow (line below the bar) to a blank image
UINavigationBar.appearance().shadowImage = UIImage()
// Sets translucent background's color
UINavigationBar.appearance().backgroundColor = .clear
// Set translucent
UINavigationBar.appearance().isTranslucent = true
return true
}
在我的 UITableViewControllerClass 中:
override func viewDidLoad() {
super.viewDidLoad()
tableView?.layer.cornerRadius = 15
//Declare a gradient
let gradient: CAGradientLayer = CAGradientLayer()
//Set the two colors of your gradient
gradient.colors = [UIColor.purple.cgColor, UIColor.red.cgColor]
//Set it's location
gradient.locations = [0.0 , 1.0]
gradient.startPoint = CGPoint(x: 0.0, y: 0.0)
gradient.endPoint = CGPoint(x: 1.0, y: 1.0)
gradient.frame = CGRect(x: 0.0, y: 0.0, width: (self.navigationController?.navigationBar.frame.size.width)!, height: (self.navigationController?.navigationBar.frame.size.height)!)
self.navigationController?.navigationBar.layer.insertSublayer(gradient, at: 0)
}
据我了解,您希望在 table 视图中使用渐变作为背景和圆角。
这是我的 ViewController.
的代码class ViewController: UIViewController {
@IBOutlet var backroundView: UIView!
@IBOutlet weak var viewContainingTableView: UIView!
@IBOutlet weak var topView: UIView!
@IBOutlet weak var navigationBar: UINavigationBar!
override func viewDidLoad() {
super.viewDidLoad()
//Change the corner radius to your liking
viewContainingTableView.layer.cornerRadius = 25
//Make the backround of your 'navigation controller' transparent
topView.backgroundColor = UIColor.white.withAlphaComponent(0.0)
//Declare a gradient
let gradient: CAGradientLayer = CAGradientLayer()
//Set the two colors of your gradient
gradient.colors = [UIColor.purple.cgColor, UIColor.red.cgColor]
//Set it's location
gradient.locations = [0.0 , 1.0]
gradient.startPoint = CGPoint(x: 0.0, y: 0.0)
gradient.endPoint = CGPoint(x: 1.0, y: 1.0)
gradient.frame = CGRect(x: 0.0, y: 0.0, width: self.view.frame.size.width, height: self.view.frame.size.height)
self.backroundView.layer.insertSublayer(gradient, at: 0)
//self.view.layer.insertSublayer(gradient, at: 0)
}
}
将 App Delegate 中的 didFinishLaunchingWithOptions 方法更改为以下内容:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
// Sets background to a blank/empty image
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
// Sets shadow (line below the bar) to a blank image
UINavigationBar.appearance().shadowImage = UIImage()
// Sets translucent background's color
UINavigationBar.appearance().backgroundColor = .clear
// Set translucent
UINavigationBar.appearance().isTranslucent = true
return true
}
这是我的视图层次结构:
这是它给出的内容:
要在没有情节提要的情况下执行此操作,请将 ViewController 代码更改为以下内容:
class ViewController: UIViewController {
var viewContainingTableView = UIView()
let navigationBarView = UINavigationBar()
let topView = UIView()
let tableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
let margins = view.layoutMarginsGuide
view.addSubview(topView)
topView.translatesAutoresizingMaskIntoConstraints = false
topView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
topView.heightAnchor.constraint(equalToConstant: 60).isActive = true
topView.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
topView.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
topView.addSubview(navigationBarView)
navigationBarView.translatesAutoresizingMaskIntoConstraints = false
navigationBarView.topAnchor.constraint(equalTo: topView.topAnchor).isActive = true
navigationBarView.bottomAnchor.constraint(equalTo: topView.bottomAnchor).isActive = true
navigationBarView.leadingAnchor.constraint(equalTo: topView.leadingAnchor).isActive = true
navigationBarView.trailingAnchor.constraint(equalTo: topView.trailingAnchor).isActive = true
self.navigationItem.title = "Test"
let buttonNavBar = UIBarButtonItem(title: "Button", style: .plain, target: nil, action: #selector(buttonAction))
navigationItem.rightBarButtonItem = buttonNavBar
navigationBarView.setItems([navigationItem], animated: false)
view.addSubview(viewContainingTableView)
viewContainingTableView.translatesAutoresizingMaskIntoConstraints = false
viewContainingTableView.topAnchor.constraint(equalTo: topView.bottomAnchor, constant: 0).isActive = true
viewContainingTableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
viewContainingTableView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
viewContainingTableView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
viewContainingTableView.backgroundColor = UIColor.white
viewContainingTableView.addSubview(tableView)
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.topAnchor.constraint(equalTo: viewContainingTableView.topAnchor, constant: 20).isActive = true
tableView.bottomAnchor.constraint(equalTo: viewContainingTableView.bottomAnchor).isActive = true
tableView.leadingAnchor.constraint(equalTo: viewContainingTableView.leadingAnchor).isActive = true
tableView.trailingAnchor.constraint(equalTo: viewContainingTableView.trailingAnchor).isActive = true
view.setNeedsUpdateConstraints()
view.updateConstraints()
//Change the corner radius to your liking
viewContainingTableView.layer.cornerRadius = 25
//Make the backround of your 'navigation controller' transparent
topView.backgroundColor = UIColor.white.withAlphaComponent(0.0)
//Declare a gradient
let gradient: CAGradientLayer = CAGradientLayer()
//Set the two colors of your gradient
gradient.colors = [UIColor.purple.cgColor, UIColor.red.cgColor]
//Set it's location
gradient.locations = [0.0 , 1.0]
gradient.startPoint = CGPoint(x: 0.0, y: 0.0)
gradient.endPoint = CGPoint(x: 1.0, y: 1.0)
gradient.frame = CGRect(x: 0.0, y: 0.0, width: self.view.frame.size.width, height: self.view.frame.size.height)
self.view.layer.insertSublayer(gradient, at: 0)
}
@objc func buttonAction() {
//Function of your button: add code to execute when button is pressed
}
}