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)
}
Related
I'm trying to add an image and scrollview programmatically to swift, and then allow them to be pinched to zoom.
I've followed the instructions from UIImageView pinch zoom swift but am still not able to pinch to zoom, I'm not sure what I'm missing
class mapViewController: UIViewController, UIScrollViewDelegate, IALocationManagerDelegate {
var imageViewBackground: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
let width = UIScreen.main.bounds.size.width
let height = UIScreen.main.bounds.size.height
let imageViewBackground = UIImageView(frame: CGRect(x:0, y:0, width:width-70, height:height-60))
var image: UIImage?
let urlString = "https://firebasestorage.googleapis.com/v0/b/coxur-59760.appspot.com/o/Maps%2FCapture.png?alt=media&token=741b1425-cb01-453b-8c40-47f6e5fd528d"
let url = NSURL(string: urlString)! as URL
if let imageData: NSData = NSData(contentsOf: url) {
image = UIImage(data: imageData as Data)
}
imageViewBackground.image = image
// you can change the content mode:
imageViewBackground.contentMode = UIView.ContentMode.scaleAspectFill
var scrollView: UIScrollView!
scrollView = UIScrollView(frame: CGRect(x: 0, y: 120, width: width, height: height))
scrollView.contentSize = CGSize(width: width, height: 2000)
scrollView.delegate = self
scrollView.minimumZoomScale = 1.0
scrollView.maximumZoomScale = 6.0
scrollView.addSubview(imageViewBackground)
self.view.addSubview(scrollView)
//self.view.sendSubviewToBack(imageViewBackground)
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return imageViewBackground
}
}
What am I doing wrong here?
The problem is this line:
let imageViewBackground = UIImageView(frame: CGRect(x:0, y:0, width:width-70, height:height-60))
Delete let. Problem solved.
Sorry do not have the reputation to comment but could it possibly be that you are initializing the scrollView inside of the viewDidLoad instead of making it a class variable?
I got UIViewController contain custom navigationbar and an UITableView
I am trying to make all the view background blur so the icons and lable and information show above blur effect
I tried this but the blur effect apply on the icons and information
this code in viewWillAppear
//Background
ChatRoomTable.backgroundColor = .clear
let backgroundImage = UIImage(named: "16.png")
let imageView = UIImageView(image: backgroundImage)
ChatRoomTable.backgroundView = imageView
ChatRoomTable.tableFooterView = UIView(frame: CGRect.zero)
ChatRoomTable.sectionIndexColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.6715271832)
let blurEffect = UIBlurEffect(style: .dark)
let blurView = UIVisualEffectView(effect: blurEffect)
blurView.frame = self.view.bounds
view.addSubview(blurView)
Update
I want background like this and every thing show above
#SemarY I got your bug you added image you at wrong place so let me give you code which will help you for your desire output.
//Background
ChatRoomTable.backgroundColor = .clear
ChatRoomTable.tableFooterView = UIView(frame: CGRect.zero)
ChatRoomTable.sectionIndexColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.6715271832)
let backgroundImage = UIImage(named: "16.png")
let imageView = UIImageView(image: backgroundImage)
imageView.frame = self.view bound
view.addSubView(imageView)
let blurEffect = UIBlurEffect(style: .dark)
let blurView = UIVisualEffectView(effect: blurEffect)
blurView.frame = self.view.bounds
view.addSubview(blurView)
self.bringSubviewToFront(ChatRoomTable)
Try adding the image to the Content View of the blurView like so blurView.contentView.addSubview(imageView)
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)
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)
How can i make a button background get blurred?
Ill found out to get a blurred effect with this:
let blur = UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffectStyle.Light))
blur.frame = buttonForDisabling.frame
self.tableView.addSubview(blur)
This is working, but only makes a blur with the same frame as my button. I would like to have a button with a blurred background. Is that possible in iOS8?
If you are using a button with an image, bring the imageview to front:
buttonForDisabling.bringSubview(toFront: buttonForDisabling.imageView!)
or just use this extension:
import Foundation
import UIKit
extension UIButton
{
func addBlurEffect()
{
let blur = UIVisualEffectView(effect: UIBlurEffect(style: .light))
blur.frame = self.bounds
blur.isUserInteractionEnabled = false
self.insertSubview(blur, at: 0)
if let imageView = self.imageView{
self.bringSubview(toFront: imageView)
}
}
}
This can be done by creating the blur as you did and inserting it below the titleLabel of the UIButton. It's important to disable user interaction in order to allow the touches to not be intercepted by the the visual effect view and forwarded to the button.
Swift 4.2:
let blur = UIVisualEffectView(effect: UIBlurEffect(style:
UIBlurEffect.Style.light))
blur.frame = buttonForDisabling.bounds
blur.isUserInteractionEnabled = false //This allows touches to forward to the button.
buttonForDisabling.insertSubview(blur, at: 0)
Used Sherwin's great answer, but it didn't quite work for me as I use auto layout and my frame was .zero while this code runs. So I modified the code to use auto-layout and this now works for me:
import Foundation
import UIKit
extension UIButton {
func addBlurEffect(style: UIBlurEffect.Style = .regular, cornerRadius: CGFloat = 0, padding: CGFloat = 0) {
backgroundColor = .clear
let blurView = UIVisualEffectView(effect: UIBlurEffect(style: style))
blurView.isUserInteractionEnabled = false
blurView.backgroundColor = .clear
if cornerRadius > 0 {
blurView.layer.cornerRadius = cornerRadius
blurView.layer.masksToBounds = true
}
self.insertSubview(blurView, at: 0)
blurView.translatesAutoresizingMaskIntoConstraints = false
self.leadingAnchor.constraint(equalTo: blurView.leadingAnchor, constant: padding).isActive = true
self.trailingAnchor.constraint(equalTo: blurView.trailingAnchor, constant: -padding).isActive = true
self.topAnchor.constraint(equalTo: blurView.topAnchor, constant: padding).isActive = true
self.bottomAnchor.constraint(equalTo: blurView.bottomAnchor, constant: -padding).isActive = true
if let imageView = self.imageView {
imageView.backgroundColor = .clear
self.bringSubviewToFront(imageView)
}
}
}
Swift 5 version of #tuvok's excellent response. I've also updated it and added options to control various aspects such as:
• style: The blur style
• cornerRadius: Allows you to make it rounded or a circle
• padding: This is useful if you want to use a cornerRadius but you want to make the size of the button larger for hit-testing.
extension UIButton {
func addBlurEffect(style: UIBlurEffect.Style = .regular, cornerRadius: CGFloat = 0, padding: CGFloat = 0) {
backgroundColor = .clear
let blurView = UIVisualEffectView(effect: UIBlurEffect(style: style))
blurView.frame = bounds.inset(by: UIEdgeInsets(horizontal: padding, vertical: padding))
blurView.isUserInteractionEnabled = false
blurView.backgroundColor = .clear
if cornerRadius > 0 {
blurView.layer.cornerRadius = cornerRadius
blurView.layer.masksToBounds = true
}
self.insertSubview(blurView, at: 0)
if let imageView = self.imageView{
imageView.backgroundColor = .clear
self.bringSubviewToFront(imageView)
}
}
}