Navigation Bar logo title not centered - swift

I've implemented a logo as the navigation bar title, using the code below:
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 150, height: 40))
imageView.contentMode = .scaleAspectFit
let image = UIImage(named: "nav_bar_icon_gradient.png")
imageView.image = image
self.navigationItem.titleView = imageView
It works however the item is fully centred on the navigation bar (see screenshot), I'm aware it could be because of the navigation bar item on the right. Does anyone know how to fix this?

Related

set background color of footerView UI TableView doesn't work

I use UIView() for remove separate line and it works but changing background color of footer view doesn't work. This is my code. Can somebody explain why? Thank you so much!
let footerView = UIView()
footerView.backgroundColor = UIColor(hexString: "#F7F9FC")
myTableView.tableFooterView = footerView
It is because the size of your footerView is zero.
let footerView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 40.0))
footerView.backgroundColor = UIColor(hexString: "#F7F9FC")
myTableView.tableFooterView = footerView
I think you must set a frame for footer view. If you just only create UIView then the default of Apple will create UIView with frame is zero. You need to set width, height, x, y for it to display in TableView
let frame = CGRect(x: 0, y: 0, width: yourWidthExpect, height: yourHeightExpect))
let footerView = UIView(frame: frame)
After that you can set background color and do anything.

swift navigation bar title image position

I created a navigation bar title image, it is working well.
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 5, height: 10))
imageView.contentMode = .scaleAspectFit
let image = UIImage(named: imageTitle)
imageView.image = image
navigationItem.titleView = imageView
but i added an item button at the right side of navigation bar, the title image move to left a bit. How to make the image title in the center of the screen?

Align Drop Down Menu Title to prevent overlapping of other buttons

I have the below image where the title of the drop down menu title is overlapped with other buttons. How to solve this issue?
Code:
func createDropDownMenu() {
// create the drop down menu
let title = prepareNavigationBarMenuTitleView()
prepareNavigationBarMenu(title)
updateMenuContentOffsets()
}
func prepareNavigationBarMenuTitleView() -> String {
// Both title label and image view are fixed horizontally inside title
// view, UIKit is responsible to center title view in the navigation bar.
// We want to ensure the space between title and image remains constant,
// even when title view is moved to remain centered (but never resized).
titleView = DropDownTitleView(frame: CGRect(x: 0, y: 0, width: 150, height: 40))
titleView.addTarget(self,
action: #selector(DocumentViewController.willToggleNavigationBarMenu(_:)),
for: .touchUpInside)
titleView.addTarget(self,
action: #selector(DocumentViewController.didToggleNavigationBarMenu(_:)),
for: .valueChanged)
titleView.titleLabel.lineBreakMode = NSLineBreakMode.byClipping
titleView.titleLabel.numberOfLines = 2
titleView.titleLabel.textColor = UIColor.black
titleView.title = currentNode.title
navigationItem.titleView = titleView
return titleView.title!
}
I had to set the frame of the TitleLable and set the numberOfLines = 0 to solve my problem.
Code:
titleView.titleLabel.frame = CGRect(x: 0, y: 0, width: 600, height: 80)
titleView.numberOfLines = 0
titleView.titleLabel.text = currentNode.title

statusBar becomes transparent when searchBar is active

I have a UISearchController in my view controller, when the searchBar is active the status bar becomes transparent. I've tried this and this but none of them worked.
This is the view hierarchy of the view controller:
How can I make the status bar translucent?
Using this in viewDidLoad solved the problem:
var frame = UIApplication.shared.statusBarFrame
frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height*3.3)
//3.3 is a practical number
let statusBarView = UIView(frame: frame)
statusBarView.backgroundColor = sharedApplication.mainThemeColor
searchController.view.addSubview(statusBarView)

Logo in Navigation Bar Swift not showing

I'm trying to add a logo to my page's navigation bar, however it doesn't appear when I added this code to my viewDidLoad():
let logo = UIImage(named: "Dog_Log_Icon_H_W")
let imageView = UIImageView(image:logo)
self.navigationItem.titleView = imageView
Does anybody know why this might not be working? I checked that the image name is correct. This is my storyboard if it helps:
So it worked when I made an outlet,
#IBOutlet weak var navItem: UINavigationItem!
then put the following code in my viewDidAppear:
let logo = UIImage(named: "Dog_Log_Icon_4"){
let imageView = UIImageView(image:logo)
imageView.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
navItem.titleView = imageView
not sure why I had to do that..
The reason is because you forgot set the position for the imageView.
You can change your code like this:
let logo = UIImage(named: "Dog_Log_Icon_H_W")
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 30, height: 30)
imageView.image = logo
self.navigationItem.titleView = imageView
You have not set the frame of the image view. I have set frame of the image view and it works fine.
if let logo = UIImage(named: "download"){
let imageView = UIImageView(image:logo)
imageView.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
self.navigationItem.titleView = imageView
}else{
print("Image not found...")
}