I wrote a function to show my app logo in the middle of the navigation bar, and it is working fine when I call it from viewDidLoad() function.
But, when I tried to add it to a UINavigationController extension, it is not working! Although the function is called, the logo does not appear.
Any idea?
Thanks
extension UINavigationController {
func showNavLogo(){
let imageW = 59
let imageH = 30
let imageView = UIImageView()
imageView.image = UIImage(named: "Logo.png")
imageView.contentMode = .scaleAspectFit
self.navigationItem.titleView = imageView
let currWidth = self.navigationItem.titleView?.widthAnchor.constraint(equalToConstant: CGFloat(imageW))
currWidth?.isActive = true
let currHeight = self.navigationItem.titleView?.heightAnchor.constraint(equalToConstant: CGFloat(imageH))
currHeight?.isActive = true
}
}
I found the solution by adding the function to UIViewController extension instead of UINavigationController
extension UIViewController {
func showNavLogo(){
let imageW = 59
let imageH = 30
let imageView = UIImageView()
imageView.image = UIImage(named: "Logo.png")
imageView.contentMode = .scaleAspectFit
self.navigationItem.titleView = imageView
let currWidth = self.navigationItem.titleView?.widthAnchor.constraint(equalToConstant: CGFloat(imageW))
currWidth?.isActive = true
let currHeight = self.navigationItem.titleView?.heightAnchor.constraint(equalToConstant: CGFloat(imageH))
currHeight?.isActive = true
}
Did you remember to call self.showNavLogo() in your viewDidload?
your extension means you have a function called showNavlogo, but you may still have to call the showlogo method by using the self..showNavLogo() in your viewDidload or viewWillAppear.
Also ensure your storyboard or View controller or nib is mapped to a Navigation controller for your viewcontroller to implement your method.
changing the extension to UIViewcontroller worked for me
Related
I am trying to make UITabBar look blur.
I am trying to make something like this in this image
But my view now looks like this
This is my view for tabbar
I tried this code in UITabbarController -
Code:
class TabBarViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
configureTabbar()
}
func configureTabbar(){
let blurEffect = UIBlurEffect(style: .dark)
let vibrancyEffect = UIVibrancyEffect(blurEffect: blurEffect)
let vibrancyView = UIVisualEffectView()
vibrancyView.frame = tabBar.bounds
vibrancyView.autoresizingMask = .flexibleWidth
vibrancyView.effect = vibrancyEffect
tabBar.insertSubview(vibrancyView, at: 0)
tabBar.isTranslucent = true
tabBar.backgroundImage = UIImage()
tabBar.backgroundColor = .clear
tabBar.barStyle = UIBarStyle.black
tabBar.barTintColor = UIColor.clear
}
System bars such as UINavigationBar, UITabBar & UIToolbar are translucent by default and you don't need to add anything extra to get that effect.
You just need to make sure that your view extends it's content under system bars. You can go to storyboard and make sure the Extend Edges - Under Bottom Bars is checked for your UIViewController that you plan to see this effect on.
with the below code i am able to get title image in navigationbar but the titlePressed method is not calling.. and if i tap title i am unable to go HomeVC.. please do help.. where am i wrong
func setUpNavigationBar(){
let logoImageView = UIImageView.init(image: #imageLiteral(resourceName: "pic-logo"))
logoImageView.tintColor = .darkGray
logoImageView.frame = CGRect(x:0.0,y:0.0, width:100,height:25.0)
logoImageView.contentMode = .scaleAspectFit
logoImageView.widthAnchor.constraint(equalToConstant: 200).isActive = true
logoImageView.heightAnchor.constraint(equalToConstant: 50).isActive = true
self.navigationItem.titleView?.isUserInteractionEnabled = true
let titleTap = UITapGestureRecognizer(target: self, action: #selector(titlePressed))
self.navigationItem.titleView?.addGestureRecognizer(titleTap)
self.navigationItem.titleView = logoImageView
}
#objc func titlePressed(){
print("title clicked...")
let vc = StoryBoard.main.instantiateViewController(identifier: "HomeVC") as! HomeVC
self.navigationController?.pushViewController(vc, animated: true)
}
self.navigationItem.titleView?.addGestureRecognizer(titleTap)
self.navigationItem.titleView = logoImageView
You add a gesture recognizer, if title view is not nil, and then on the next line you change the title view right away to a different thing altogether, which won't have a gesture recognizer on it.
self.navigationItem.titleView = logoImageView
logoImageView.addGestureRecognizer(titleTap)
This line:
self.navigationItem.titleView?.isUserInteractionEnabled = true
Is also problematic because: titleView is likely nil at this point, so this line will do nothing, and you change the title view later anyway.
I created a CollectionView. This view has a userProfileImageView and I want to change view controller to ProfilePage when clicked this user profile image. Try some code but neither present nor push view controller doesn't work to me. Can you help me ?
lazy var userProfileImageView: UIImageView = {
let useriv = UIImageView()
useriv.contentMode = .scaleAspectFill
useriv.clipsToBounds = true
useriv.layer.cornerRadius = 40 / 2
useriv.isUserInteractionEnabled = true
useriv.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(userImageTap)))
return useriv
}()
#objc func userImageTap() {
print("useriv clicked")
let profilePage = ProfilePage(collectionViewLayout: UICollectionViewFlowLayout())
}
I have the following problem. I have about 10 different images which are in a view controller. Now I want the image to switch to fullscreen when they are tapped on. I already set the allow user interaction option to yes, and put in the following code i found on this website:
import UIKit
class ImageViewController: UIViewController {
#IBAction func imageTapped(sender: UITapGestureRecognizer) {
let imageView = sender.view as! UIImageView
let newImageView = UIImageView(image: imageView.image)
newImageView.frame = self.view.frame
newImageView.backgroundColor = .blackColor()
newImageView.contentMode = .ScaleAspectFit
newImageView.userInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: "dismissFullscreenImage:")
newImageView.addGestureRecognizer(tap)
self.view.addSubview(newImageView)
}
func dismissFullscreenImage(sender: UITapGestureRecognizer) {
sender.view?.removeFromSuperview()
}
Does anyone know why my images are not tappable and turning fullscreen?
Make sure both the image view have userInteractionEnabled = YES and
here's an example for what you are trying to do here https://github.com/abhinavsingh77/ImagePreview
I'm trying to add SearchController to UINavigationBar. I'm trying to set UITextField of UISearchBar after back button of UINavigationController. I want some more space after back button
When I start searching it appears as
Whereas I should be able to view back button. Only textfield width should be decreased. Also after Cancel it should be again back to initial layout. Whereas it is displayed as below:
Below is my code
var searchResultController = UISearchController()
self.searchResultController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
controller.searchBar.delegate = self
controller.hidesNavigationBarDuringPresentation = false
self.navigationController?.navigationBar.addSubview(controller.searchBar)
return controller
})()
override func viewDidAppear(animated: Bool) {
for subView in searchResultController.searchBar.subviews{
for subsubView in subView.subviews {
if let textField = subsubView as? UITextField {
var bounds: CGRect
bounds = textField.frame
bounds.size.width = self.view.bounds.width - 50
}
}
}
}
Please let me know how can I fix this.
Thanks in advance
For setting the UIsearchBar add it to Navigation's titleView as
self.navigationItem.titleView = controller.searchBar
For removing Cancel button we can use UISearchControllerDelegate method
func didPresentSearchController(searchController: UISearchController) {
searchController.searchBar.showsCancelButton = false
}
Hope this may help any one.