Navigation Bar will not become transparent - swift

I need the navigation bar to become transparent. Here is what i have put in my ViewController:
class ViewController: UIViewController {
#IBOutlet weak var navbar: UINavigationBar!
override func viewDidLoad() {
super.viewDidLoad()
navbar.isTranslucent=true
navbar.shadowImage = UIImage()
navbar.backgroundColor = UIColor.clear
}
According to debug view hierarchy UIVisualEffectBackdropView, UIVisualEffectSubview and UIVisualEffectSubview(yes two of them) are preventing it from becoming transparent.
Ps: I am not using a Navigation Controller.
How can i fix it?

If you build with the lastest beta iOS 13.4 and XCode 11.4, the xmhafiz answer won't work anymore.
I've found another way, maybe it's just a bug in the beta software, but I'm writing it down there, just in case
import UIKit
class TransparentNavBar :UINavigationBar {
override func awakeFromNib() {
super.awakeFromNib()
self.setBackgroundImage(UIImage(), for: .default)
self.shadowImage = UIImage()
self.isTranslucent = true
self.backgroundColor = .clear
if #available(iOS 13.0, *) {
self.standardAppearance.backgroundColor = .clear
self.standardAppearance.backgroundEffect = .none
self.standardAppearance.shadowColor = .clear
}
}
}

This three lines should be enough to make your UINavigationBar transparent.
override func viewDidLoad() {
super.viewDidLoad()
// make transparent the navbar
navbar.setBackgroundImage(UIImage(), for: .default)
navbar.shadowImage = UIImage()
navbar.isTranslucent = true
}

Related

Back Button Tint Color

How can I change the back button of a certain navigation controller. I have tried to use
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.backBarButtonItem?.tintColor = UIColor.red
}
I know that if i use navigationController it will change the back button tint color on all of my view controllers.
Try this!!
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.tintColor = .red
}
override func willMove(toParent parent: UIViewController?) {
self.navigationController?.navigationBar.tintColor = // original color
}
}

Swift - UISearchController Does Not Activate - No Cursor - No Keyboard

I have a UICollectionView with a header view inside of it and a UISearchController in the title of the navigation bar. This code has worked for other views yet does not work for this UICollectionView. I wanted to throw this out to the SO community to see if they can help me solve this problem.
class ViewBusinessProfile: UICollectionViewController, UICollectionViewDelegateFlowLayout, CLLocationManagerDelegate, UISearchControllerDelegate, UISearchBarDelegate, UISearchResultsUpdating {
#IBOutlet var collectionViews: UICollectionView!
var searchController : UISearchController!
override func viewDidLoad() {
super.viewDidLoad()
self.searchController = UISearchController(searchResultsController: nil)
self.searchController.searchResultsUpdater = self
self.searchController.delegate = self
self.searchController.searchBar.delegate = self
self.searchController.hidesNavigationBarDuringPresentation = false
self.searchController.dimsBackgroundDuringPresentation = true
self.searchController.obscuresBackgroundDuringPresentation = false
self.searchController.searchBar.placeholder = "search products..."
//self.searchController.automaticallyShowsCancelButton = false
self.searchController.definesPresentationContext = true
searchController.searchBar.becomeFirstResponder()
navigationItem.titleView = searchController.searchBar
collectionViews.dataSource = self
collectionViews.delegate = self
}
func updateSearchResults(for searchController: UISearchController) {
}
What can I do as far as debugging or anything to try to track down this problem? I have re-created the view in a new UICollectionView and the same problem occurs. The SearchBar is visible but not active to touch yet Back button works on the navigation bar.
These three views all have UISearchControllers on them and I segue between them. The one all the way on the right is where this problem is occurring. The same UISearchController code is used on the left two views and its works perfectly.
Based on what I saw in this related solution, I came up with this code:
import UIKit
class SecondViewController: UIViewController, UISearchControllerDelegate, UISearchBarDelegate {
var searchController : UISearchController!
override func viewDidLoad() {
super.viewDidLoad()
addNavigationBarItems()
}
func addNavigationBarItems() {
self.searchController = searchControllerWith(searchResultsController: nil)
navigationItem.titleView = self.searchController.searchBar
self.definesPresentationContext = true
}
func searchControllerWith(searchResultsController: UIViewController?) -> UISearchController {
let searchController = UISearchController(searchResultsController: searchResultsController)
searchController.delegate = self
// searchController.searchResultsUpdater = self
searchController.searchBar.delegate = self
searchController.hidesNavigationBarDuringPresentation = false
// dimsBackgroundDuringPresentation is deprecated as of iOS 12.0
searchController.dimsBackgroundDuringPresentation = true
searchController.searchBar.searchBarStyle = .prominent
searchController.searchBar.backgroundImage = UIImage()
searchController.searchBar.barTintColor = UIColor(red: 10/255, green: 150/255, blue: 255/255, alpha: 1) //rgb(10, green: 150, blue: 255)
return searchController
}
func updateSearchResults(for searchController: UISearchController) {
}
}
This assumes a navigation bar already exists in the parent view controller (i.e. you don't have to create a new NavigationBar), and results should come out looking like this:

How to use “Look Up in Dictionary” in NSPopover Swift

I'm building my macOS app that contains NSPopover with NSTextView inside. If "Look up & data detectors" is active at system preferences, NSTextView will support this functionality without any additional settings. But in case, when you place NSTextView inside of NSPopover, this function won't work anymore. Is there any way to fix this problem without NSTextView.showDefenition(for: at:)?
How it usually works
Same thing, but won't work with NSTextView that placed inside of NSPopover
I've started new test project just to check behaviour, so code is simple as it is.
Main Controller:
import Cocoa
class ViewController: NSViewController {
#IBOutlet weak var popoverButton: NSButton!
#IBAction func pressButton(_ sender: NSButton) {
let popover = NSPopover()
popover.contentViewController = PopoverController()
popover.contentSize = CGSize(width: 470, height: 300)
popover.show(relativeTo: popoverButton.bounds, of: popoverButton, preferredEdge: NSRectEdge.minY)
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
Popover Controller:
import Cocoa
class PopoverController: NSViewController {
var textView = NSTextView()
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear() {
super.viewWillAppear()
view.addSubview(textView)
textView.translatesAutoresizingMaskIntoConstraints = false
textView.frame = view.bounds
}
}

How to make Horizontal scroll segment in swift

when we scroll segment horizontally then the segment and regarding viewcontroller should scroll move horizontally
for that i have tried SJSegmentedScrollView
i have installed cocoapod and added below code:
code: but here even segment not coming.. where i am mistake.. how to add segment and viewcontrollers horizontally
or is there any other thirdparty library for segment please do help here
import UIKit
import SJSegmentedScrollView
class SegmentViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.navigationController?.isNavigationBarHidden = true
if let storyboard = self.storyboard {
let headerViewController = storyboard
.instantiateViewController(withIdentifier: "SegmentViewController")
let firstViewController = storyboard
.instantiateViewController(withIdentifier: "FirstTableViewController")
firstViewController.title = "First"
let secondViewController = storyboard
.instantiateViewController(withIdentifier: "SecondTableViewController")
secondViewController.title = "Second"
let thirdViewController = storyboard
.instantiateViewController(withIdentifier: "ThirdTableViewController")
thirdViewController.title = "Third"
self.present(thirdViewController, animated: false, completion: nil)
let segmentedViewController = SJSegmentedViewController(headerViewController: headerViewController,
segmentControllers: [firstViewController,
secondViewController,
thirdViewController])
addChild(segmentedViewController)
self.view.addSubview(segmentedViewController.view)
segmentedViewController.view.frame = self.view.bounds
segmentedViewController.didMove(toParent: self)
}
}
}
I think no need to the using third party frameworks for every development. Try native ways first. For example you can do something like this by using UIKit, UISegmentedControl Apple Document
class ViewController: UIViewController {
#IBOutlet weak var segmentedControl: UISegmentedControl!
#IBOutlet weak var textLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func segmentedControlPressed(_ sender: UISegmentedControl) {
switch segmentedControl.selectedSegmentIndex {
case 0: textLabel.text = "First";
case 1: textLabel.text = "Second";
case 2: textLabel.text = "Third";
default: break;
}
}
}

How to modified object of child viewcontroller from parent viewcontroller from in swift

I want to modify an object of child UIViewController but it is not working. Can anyone solve my problem? thanks a lot ~~
below is my code
My ViewController class:
import UIKit
class ViewController: UIViewController {
var x:UIView?
override func viewDidLoad() {
super.viewDidLoad()
var sec = second()
sec.x?.backgroundColor = UIColor.redColor()
view.addSubview(sec.view)
self.addChildViewController(sec)
}
}
My second class:
class second:UIViewController{
var x:UIView!
override func viewDidLoad() {
super.viewDidLoad()
x = UIView(frame: CGRectMake(55, 55, 100, 100))
x!.backgroundColor = UIColor.greenColor()
self.view.addSubview(x!)
}
}
This is happening because the view is probably nil when you are attempting to set its background color. Just add a background color property inside the second view controller and set that. Then inside viewDidLoad set the second view controller's background color from there.
Something like this
class ViewController: UIViewController {
var x:UIView?
override func viewDidLoad() {
super.viewDidLoad()
var sec = second()
sec.backColor = UIColor.redColor()
view.addSubview(sec.view)
self.addChildViewController(sec)
}
}
And then:
class second:UIViewController {
var backColor = UIColor.greenColor()
var x:UIView!
override func viewDidLoad() {
super.viewDidLoad()
x = UIView(frame: CGRectMake(55, 55, 100, 100))
x.backgroundColor = backColor
self.view.addSubview(x)
}
}
After initializing second view controller, call addChildViewController(instanceOfSecondVC)
And after adding it as subview, call sec.didMoveToParentViewController(self).
So your code will be like:
import UIKit
class ViewController: UIViewController {
var x:UIView?
override func viewDidLoad() {
super.viewDidLoad()
var sec = second()
addChildViewController(sec)
sec.x?.backgroundColor = UIColor.redColor()
view.addSubview(sec.view)
self.addChildViewController(sec)
sec.didMoveToParentViewController(self)
}
}