I would like to use blur background for my view controller but as soon as the view is loaded, the whole thing turns into gray.
I present the view controller by performing a segue from another view, and in the viewDidLoad() method of the view controller, I implement the blur effect as given below
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.clear
let blurBgView = UIVisualEffectView(effect: UIBlurEffect(style: .extraLight))
blurBgView.translatesAutoresizingMaskIntoConstraints = false
view.insertSubview(blurBgView, at: 0)
NSLayoutConstraint.activate([
blurBgView.heightAnchor.constraint(equalTo: view.heightAnchor),
blurBgView.widthAnchor.constraint(equalTo: view.widthAnchor),
])
}
This is how it looks like
How can I implement a blur background for my view controller?
I use XCode 9.3, Swift 4.1
Thanks
Present your view controller and set its modalPresentationStyle from your initial vc:
let newController = NewViewController()
newController.modalPresentationStyle = .overCurrentContext
present(newController, animated: true, completion: nil)
Or, if you are preparing for the segue:
let newController: NewViewController = segue.destination as! NewViewController
newController.modalPresentationStyle = .overCurrentContext
And in the NewViewController class in viewDidLoad() add your code.
Related
I want to make the screen like the following.
In source class, I write this code and push to side menu controller.
let vc = self.storyboard?.instantiateViewController(withIdentifier: "SidemenuController")as! SidemenuController
self.navigationController?.pushViewController(vc, animated: false)
I write the following code in destination controller view did load method but just color opacity is decreased data is not visible.
override func viewDidLoad() {
self.view.backgroundColor = self.hexStringToUIColor(hex: "BE8790").withAlphaComponent(0.4)
}
Instead of using .withAlphaComponent(0.4)
Try this Adjust the number as needed
self.view.alpha = 0.5
You show present your SideViewController. Maybe custom how it shows
let sideVC = // Your init
sideVC.modalPresentationStyle = .fullScreen
present(sideVC, animated: true, completion: nil)
In your SideViewController,
override func viewDidLoad() {
super.viewDidLoad()
view.alpha = 0.5
view.backgroundColor = // Your color
tableView.backgroundColor = .clear
}
Now I have two Controller, one is FirstViewController,and one is SecondViewController.
In FirstViewController
var secondViewController: SecondViewController!
let textView: UITextView = {
let textView = UITextView()
//textView.font = UIFont.systemFont(ofSize: 20)
textView.isEditable = true
textView.keyboardDismissMode = .interactive
textView.allowsEditingTextAttributes = true
return textView
}()
override func viewDidLoad() {
super.viewDidLoad()
secondViewController = SecondViewController()
secondViewController.firstViewController = self
setUpLayout()
addKeyboardObserver()
// Do any additional setup after loading the view, typically from a nib.
}
then I add a button to show secondViewController's view as a custom keyboard
#objc func showSecondView() {
textView.resignFirstResponder()
textView.inputView = secondViewController.view
textView.becomeFirstResponder()
}
then in SecondViewController, as the pic show, now I wanna add a navigation bar in SecondViewController, how?(I can do it using storyboard, but I can't do it using code).
and I wanna show the ThirdViewController when the tableView in SecondViewController(the red area) is selected, and the ThirdViewController's view will show in the red area, not the whole screen,(just like the storyboard's "show" connection) how can I do that?
You have to take navigation controller from root view controller so you can hide and show whenever you want, Another thing you can do is, take navigation view controller for particular view controller. But you can not navigate (pushing) any navigation view controller. You can only present it. Check my code below
import UIKit
class ViewControllerWithoutNavigation: UIViewController { // This is without navigation
override func viewDidLoad() {
super.viewDidLoad()
}
func gotoNextViewController(){
let nvc = UINavigationController(rootViewController: ViewControllerWithNavigation())
let transition = CATransition()
transition.duration = 0.25
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromRight
self.view.window!.layer.add(transition, forKey: kCATransition)
//transition is to show like pushing, Without it it will look like presenting from bottom
self.present(nvc, animated: false, completion: nil)
}
}
class ViewControllerWithNavigation: UIViewController { // This is with navigation
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
}
}
Adding NavigationController
Select the viewcontroller in storyboard and goto Editor -> Embedd In -> Navigation Controller
To show another view controller
*Add storyboard id to Navigation Controller
*Then add this code in action
let storyBoard = UIStoryboard(name: "storyboard_name", bundle: nil)
let destination = storyBoard.instantiateViewController(withIdentifier: "storyboard_id")
self.navigationController?.pushViewController(destination, animated: true)
I'm new to Xcode/Swift. I have a center button I want to present a modal view from the current view (tab).
I've implemented a custom class for UITabBar that adds that button according to this guide.
This UITabBar has no view controller in self that I can find, so I get an error when I try to present a view.
Error: Value of type 'MainTabBar' has no member 'present'
Is there a way I can have this button present modal from the current view?
I'm not sure how to reach the current view from this custom class.
Should I point the button's addTarget to a uitabbar Delegate and watch for it in my other views?
Should I stop doing this and just do tabs with a tab that pulls up my modal view in a ViewDidAppear() on a different view?
I think I lose the ability to pop, or dismiss, back to the last view if I do that.
Here's the custom UITabBar class I'm working with.
class MainTabBar: UITabBar {
private var middleButton = UIButton()
override func awakeFromNib() {
super.awakeFromNib()
setupMiddleButton()
}
func setupMiddleButton() {
middleButton.frame.size = CGSize(width: 70, height: 70)
middleButton.backgroundColor = .blue
middleButton.layer.cornerRadius = 35
middleButton.layer.masksToBounds = true
middleButton.center = CGPoint(x: UIScreen.main.bounds.width / 2, y: 0)
middleButton.addTarget(self, action: #selector(test), for: .touchUpInside)
addSubview(middleButton)
}
#objc func test() {
let vc = createController()
vc.modalTransitionStyle = .crossDissolve
self.present(vc, animated: true, completion: nil)
}
}
Found a better implementation.
Created a custom class inherited from UITabBarController and named it MainViewController.
Setup the middle button in that controller instead of in the MainTabBar custom class.
self now has member present.
#objc func test() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc =
storyboard.instantiateViewController(withIdentifier: "createModal") as! createController
self.present(vc, animated: true, completion: nil)
}
https://github.com/drrost/Custom-Tabbar-Cetner-Button
I have a UIViewController that only shows a UIView at the bottom with a fixed height:
fileprivate lazy var box: UIView = {
let box = UIView()
box.backgroundColor = UIColor.blue
return box
}()
And I'd like to set a clear color to the rest of the view (self.view). I'm presenting the view controller from a navigation controller. I'm trying this:
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.clear
view.isOpaque = false
configView()
configConstraints()
}
The subview is displayed in blue but the rest of the view is opaque white, I need it to be transparent to see the previous viewController's view.
What can I be missing?
To have transparent Model view controller background you have to set UIViewController providesPresentationContextTransitionStyle and definesPresentationContext properties while presenting the view controller.
Example
if let vc = self.storyboard?.instantiateViewController(withIdentifier: "YOUR_VIEW_CONTROLLER_ID") {
vc.providesPresentationContextTransitionStyle = true
vc.definesPresentationContext = true
vc.modalPresentationStyle=UIModalPresentationStyle.overCurrentContext
self.present(vc, animated: true, completion: nil)
}
I am relatively new to swift and iOS and can't find an answer or any help for my problem that works.
I want to create a navigation controller view when I click on a button in my previous view. My previous view is an on-boarding with a button on the last page. I successfully connected the button to my next view but I am not able to make the view act like a navigation controller. For example, when a navigation bar is displayed, I am not able to add navigation items to it.
Is there a way to set the new view I create by clicking my button in the first view to be the root controller for my navigation view?
A short version of my code:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(frame: CGRect(x: view.frame.width / 2, y: view.frame.height / 2, width: 40, height: 40))
button.backgroundColor = .red
button.tintColor = .white
button.setTitle("Test", for: .normal)
button.addTarget(self, action: #selector(change), for: .touchUpInside)
view.addSubview(button)
}
func change () {
let otherView = SecondViewController()
self.present(otherView, animated: true, completion: nil)
}
}
class SecondViewController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .yellow
}
}
The SecondViewController gets displayed with a navigation bar but I had problems implementing navigation items, because they weren't shown.
Does the second view controller I present have to be of type UIViewController or UINavigationController?
I am aware that there are easier ways to achieve my goal with the use of the storyboard but in my opinion I understand it better by making my own references via code instead of creating them by dragging them out of the storyboard.
Edit: I have no Objective-C background and learning Swift for about 4 weeks now.
You can add UINavigationController like below :
First you have to create object of SecondViewController,
let myViewController: SecondViewController? = storyboard?.instantiateViewController(withIdentifier: "SecondViewController")
Or
let myViewController: SecondViewController? = SecondViewController(nibName: "SecondViewController", bundle: nil)
Or
let myViewController: SecondViewController? = SecondViewController()
Then add Navigation to SecondViewController
let myNavigationController = UINavigationController(rootViewController: myViewController!)
If you want to present then use :
self.present(myNavigationController, animated: true) {
}
If you want to push then use :
self.navigationController?.pushViewController(myNavigationController, animated: true)
If you want to set as root controller then use :
let appDelegate: AppDelegate = (UIApplication.shared.delegate as? AppDelegate)!
appDelegate.window?.rootViewController = myNavigationController
var objVC: UIViewController? = storyboard.instantiateViewController(withIdentifier: "ViewController")
var aObjNavi = UINavigationController(rootViewController: objVC)
Now, instead of using view controller object use navigation controller object.