How to add UIVibrancyEffect to an existing UILabel ( IBOutlet )? - swift

I have a working ViewController with working UIBlurEffect. Here is my code:
#IBOutlet weak var nameLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let blurEffect = UIBlurEffect(style: .Light)
let blurView = UIVisualEffectView(effect: blurEffect)
blurView.frame = self.view.frame
blurView.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.insertSubview(blurView, atIndex: 0)
}
Now I would like to add a UIVibranyEffect to the nameLabel.
How to add UIVibrancyEffect programatically to an existing UILabel?

You need to instantiate an UIVisualEffectView and add whatever you want to be vibrant inside it. For example:
// Blur Effect
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = view.bounds
view.addSubview(blurEffectView)
// Vibrancy Effect
let vibrancyEffect = UIVibrancyEffect(forBlurEffect: blurEffect)
let vibrancyEffectView = UIVisualEffectView(effect: vibrancyEffect)
vibrancyEffectView.frame = view.bounds
// Label for vibrant text
let vibrantLabel = UILabel()
vibrantLabel.text = "Vibrant"
// Add label to the vibrancy view
vibrancyEffectView.contentView.addSubview(vibrantLabel)
// Add the vibrancy view to the blur view
blurEffectView.contentView.addSubview(vibrancyEffectView)

Related

Navigation bar display with UIImage and UILabel for title

I want to put navigation bar title with UIImage and UILabel.
let image = UIImage(systemName: "flame")
imageView.tintColor = R.color.fire()
self.navigationItem.titleView = imageView
self.navigationItem.title = "표시한 제품"
self.navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: R.color.myPageMenu]
This is my code, but this code only shows UIImage(systemName: "flame").
let image = UIImage(systemName: "flame")
let title = "표시한 제품"
let imageView = UIImageView()
imageView.image = image
let textLabel = UILabel()
textLabel.text = text
textLabel.textAlignment = .center
//Stack View
let stackView = UIStackView()
stackView.axis = NSLayoutConstraint.Axis.horizontal
stackView.distribution = UIStackView.Distribution.equalSpacing
stackView.alignment = UIStackView.Alignment.center
stackView.spacing = 4.0
stackView.addArrangedSubview(imageView)
stackView.addArrangedSubview(textLabel)
self.navigationItem.titleView = stackView
Here is how you can add UIImageView and UILabel in the Navigation bar.
File: ViewController.swift
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var navView: UIView!
#IBOutlet weak var navTitle: UILabel!
#IBOutlet weak var navImage: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
setNavTitleView()
}
func setNavTitleView() {
navTitle.text = "표시한 제품"
navTitle.textColor = .lightGray
navImage.image = UIImage(systemName: "flame")
navImage.tintColor = .red
navigationController?.navigationBar.backgroundColor = .black
navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.lightGray]
navigationController?.navigationBar.tintColor = .lightGray
navigationItem.titleView = navView
}
}
Storyboard:
Output:

Adding UIView as a subview to a UIButton and bring it to front is not working properly

I have a UIButton, the background color is white.
#IBOutlet weak var buttonNewPost: UIButton! {
didSet {
buttonNewPost.layer.borderColor = UIColor(red:0.88, green:0.88, blue:0.88, alpha:1.0).cgColor
buttonNewPost.layer.cornerRadius = 5
buttonNewPost.backgroundColor = .white
}
}
I want to add an UIView to add a shadow to this rounded button:
let buttonShadow = UIView()
buttonShadow.frame.size.width = buttonNewPost.layer.bounds.width
buttonShadow.frame.size.height = buttonNewPost.layer.bounds.height
buttonShadow.backgroundColor = .clear
buttonShadow.dropShadowEdged = true
buttonShadow.isUserInteractionEnabled = false
buttonNewPost.addSubview(buttonShadow)
buttonShadow.bringSubviewToFront(buttonNewPost)
The result is like this:
Why is the UIButton not in the front with the white background color? When I change the backgroundcolor of the UIButton to blue:
Why is it like that? I just want a white button with a shadow
You can apply shadow directly to UIButton:-
#IBOutlet weak var Btn: UIButton! {
didSet {
Btn.layer.borderColor = UIColor(red:0.88, green:0.88, blue:0.88, alpha:1.0).cgColor
Btn.layer.cornerRadius = 5
Btn.backgroundColor = .white
}
}
Add below code to Viewdidload:-
Btn?.layer.borderColor = UIColor.black.cgColor
Btn?.layer.borderWidth = 1.0
Btn?.layer.cornerRadius = 20.0
Btn?.layer.shadowOpacity = 0.5
Btn?.layer.shadowColor = UIColor.red.cgColor
Btn?.layer.shadowRadius = 5.0
Btn?.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
After using this code you will get result like this

A Blur on an image

I tried this code on an image to blur it ... using TViOS 10.1 and Swift 3.0
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.extraLight)
let blurView = UIVisualEffectView(effect: blurEffect)
blurView.frame = CGRect(x: 256, y: 128, width: 1024, height: 512)
self.view.addSubview(blurView)
With this result... sorry this is not a blur... I am missing something here?
Your code is right, Default blur view in iOS is work like this. You want more light blur view, then you should use any third party frameworks.
I used this code in my project:
#IBOutlet var blurView: UIVisualEffectView!
override func viewDidLoad() {
super.viewDidLoad()
let blurEffect = UIBlurEffect(style: .extraDark)
self.blurView.effect = blurEffect
}
And the result is:
func blurEffect(){
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = self.blurView.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.blurView.addSubview(blurEffectView)
}

Same color for StatusBar and NavigationBar iOS

I added navigationBar manualy from controls
How to make StatusBar Backgroud color = same as navigationBar
Try this code: Tested in Swift 3
#IBOutlet weak var navBar: UINavigationBar!
override func viewDidLoad() {
super.viewDidLoad()
navBar.barTintColor = UIColor.black // Set any colour
navBar.isTranslucent = false
navBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white, NSFontAttributeName:UIFont(name:"HelveticaNeue", size: 16)!]
let barView = UIView(frame: CGRect(x:0, y:0, width:view.frame.width, height:UIApplication.shared.statusBarFrame.height))
barView.backgroundColor = UIColor.black
view.addSubview(barView)
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
Updated to clear down voters

UIBlurEffect not working as expected on my UIView

I have a UIView and I set its frame in my viewDidLoad. I then use a blurBackgroundForView function to blur it:
override func viewDidLoad() {
super.viewDidLoad()
view.frame = CGRectMake(0, superView.frame.maxY-height, superView.frame.width, height)
// The view is supposed to peak in from the bottom
AnimationHelper.blurBackgroundForView(view)
}
static func blurBackgroundForView(view: UIView!){
view.backgroundColor = .clearColor()
let blurEffect = UIBlurEffect(style: .Light)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = view.frame
view.insertSubview(blurEffectView, atIndex: 0)
}
}
The issue is that the background of the UIView is NOT blurred.
When I move the function call of blurBackgroundView(view) above the view.frame = ...
I get a blurred view, but the blurred view is much to large.
The frame for blurEffectView should be CGRectMake(0, 0, superview.frame.width, height)