Auto-resized navigationBar title misplaced - ios

I have a UINavigationBar with a title of variable length. To make sure the title fits without being truncated I've implemented the following code in my viewDidLoad():
let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 40))
titleLabel.text = "\(petsName)'s Day"
titleLabel.font = UIFont.systemFont(ofSize: 30)
titleLabel.backgroundColor = UIColor.clear
titleLabel.textColor = UIColor.white
titleLabel.adjustsFontSizeToFitWidth = true
titleLabel.minimumScaleFactor = 0.5
self.navBar.topItem?.titleView = titleLabel
However, because I have a bar button item, the title is being moved over to the left:
Is there any way to implement the code above but keep the text center aligned within the navigation bar?
Please help me to resolve this issue

Try this.. may be worked
var view = UIView(frame: CGRect(x: 0, y: 0, width: 150, height: 40))
var label = UILabel(frame: CGRect(x: 0, y: 0, width: 150, height: 40))
label.text = "Joys Day"
label.textAlignment = .center
label.font = UIFont.systemFont(ofSize: 30)
view.addSubview(label)
navigationItem?.titleView = view

Change titleLabel width to device width and align it to center. Try this:
let deviceWidth = UIScreen.main.bounds.size.width
let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: deviceWidth, height: 40))
titleLabel.text = "My Test Title"
titleLabel.font = UIFont.systemFont(ofSize: 20)
titleLabel.backgroundColor = UIColor.clear
titleLabel.textColor = UIColor.white
titleLabel.adjustsFontSizeToFitWidth = true
titleLabel.minimumScaleFactor = 0.5
titleLabel.textAlignment = .center
self.navigationItem.titleView = titleLabel
Output:

Related

UINavigationItem TitleView Cutting Off When Using UIView

I am trying to display a multi-line titleView in my app. Here is my code to set the titleView.
self.titleView = TitleView(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: self.tableView.frame.width, height: 80)), title: "NEWS", subtitle: Date.dateAsStringForNavBarSubTitle())
self.titleView.backgroundColor = UIColor.red
self.navigationItem.titleView = self.titleView
The part of the implementation of TitleView is shown below:
private func setupUI() {
let label = UILabel(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: self.frame.width, height: self.frame.height)))
label.numberOfLines = 2
label.text = "\(self.title)\n\(self.subtitle)"
self.addSubview(label)
}
The label comes out to be outside the parent view.
Should I just use a UILabel instead of putting it in UIView?
I can see that you are adding label subview into title view. Instead, you can try this in your viewcontroller's viewDidLoad() method.
For Swift 4
let label = UILabel(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.width, height: 44.0))
label.backgroundColor = .red
label.numberOfLines = 2
label.textAlignment = .left
label.text = "\(self.title)\n\(self.subtitle)"
self.navigationItem.titleView = label
For more information please refer this link UINavigationBar multi-line title

Unexpected behavior for adjustsFontSizeToFitWidth

I'm wondering why my UILabel isn't resizing the font correctly when the numberOfLines is set to 1. I have a playground set up with the code below:
import UIKit
import PlaygroundSupport
let labelFrame = CGRect(x: 0, y: 0, width: 100, height: 100)
let label = UILabel(frame: labelFrame)
label.backgroundColor = .lightGray
label.text = "Something that's pretty long"
label.baselineAdjustment = .alignCenters
label.adjustsFontSizeToFitWidth = true
label.numberOfLines = 1
label.minimumScaleFactor = 0.1
let frame = CGRect(x: 0, y: 0, width: 300, height: 300)
let view = UIView(frame: frame)
view.backgroundColor = .white
view.addSubview(label)
PlaygroundPage.current.liveView = view
And this is the result
However, when I change the numberOfLines to 2 the font shrinks just like I'd expect:
import UIKit
import PlaygroundSupport
let labelFrame = CGRect(x: 0, y: 0, width: 100, height: 100)
let label = UILabel(frame: labelFrame)
label.backgroundColor = .lightGray
label.text = "Something that's pretty long"
label.baselineAdjustment = .alignCenters
label.adjustsFontSizeToFitWidth = true
label.numberOfLines = 2 // <<<<<< this is the only change
label.minimumScaleFactor = 0.1
let frame = CGRect(x: 0, y: 0, width: 300, height: 300)
let view = UIView(frame: frame)
view.backgroundColor = .white
view.addSubview(label)
PlaygroundPage.current.liveView = view
And here's the result:
What's going on here? Is this a bug or am I doing something wrong?
Hmmmm it looks like this is just a bug in Playgrounds. If I add this line
label.lineBreakMode = .byTruncatingMiddle
everything works as expected. Actually, ANY of these work:
label.lineBreakMode = .byCharWrapping
label.lineBreakMode = .byClipping
label.lineBreakMode = .byTruncatingHead
label.lineBreakMode = .byTruncatingMiddle
label.lineBreakMode = .byWordWrapping
the one that doesn't work (and is also the default) is
label.lineBreakMode = .byTruncatingTail
When I tried this in a real simulator everything looked good, so I assume I just ran into a Playgrounds bug.

how to set different texts to labels with tags in a scrollview in swift 3?

homeScrollView.frame = CGRect(x: 0, y: 0, width: screenWidth, height:
homeView.bounds.size.height)
for i in 0 ..< 3 {
scrollBtnLbl1 = UILabel()
scrollBtnLbl2 = UILabel()
scrollBtnLbl3 = UILabel()
scrollViewStartHeight = scrollViewStartHeight+50
scrollBtnLbl1.frame = CGRect(x: (screenWidth-450)*1/4, y: scrollViewStartHeight+180, width: 150, height: 30)
scrollBtnLbl1.text = "about worldcup"
scrollBtnLbl1.font = UIFont(name: "Metropolis-Regular", size: 15)
scrollBtnLbl1.textColor = UIColor.black
scrollBtnLbl1.textAlignment = .center
scrollBtnLbl1.tag = i
homeScrollView.addSubview(scrollBtnLbl1)
scrollBtnLbl2.frame = CGRect(x: (screenWidth-450)*2/4 + 150, y: scrollViewStartHeight+180, width: 150, height: 30)
scrollBtnLbl2.text = "top goals"
scrollBtnLbl2.font = UIFont(name: "Metropolis-Regular", size: 15)
scrollBtnLbl2.textColor = UIColor.black
scrollBtnLbl2.textAlignment = .center
scrollBtnLbl2.tag = i
homeScrollView.addSubview(scrollBtnLbl2)
scrollBtnLbl3.frame = CGRect(x: (screenWidth-450)*3/4 + 300, y: scrollViewStartHeight+180, width: 150, height: 30)
scrollBtnLbl3.text = "top goals"
scrollBtnLbl3.font = UIFont(name: "Metropolis-Regular", size: 15)
scrollBtnLbl3.textColor = UIColor.black
scrollBtnLbl3.textAlignment = .center
scrollBtnLbl3.tag = i
homeScrollView.addSubview(scrollBtnLbl3)
scrollViewStartHeight = scrollViewStartHeight+300
}
I have 9 labels in scrollview which is homescrollview and I want to give different texts to each of them. I have assigned tags to each of it, but I don't know how to access those labels with tags. any help is appreciated, thanks in advance.
You can access them by viewWithTag method:
if let lbl = self.view.viewWithTag(1 /*your UILabel tag*/) as? UILabel {
lbl.text = "some text"
}

Swift Add UIView with subviews to titleView

I'd like to customize my navigation bar and insert a UIView with two subviews into the title view of the navigation bar.
Unfortunately the subviews aren't displayed.
Do you have any idea why?
let titleContainer = UIView()
titleContainer.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height)
let searchIcon = UIButton()
searchIcon.setImage(UIImage(named: "search_icon"), for: UIControlState.normal)
searchIcon.layer.frame = CGRect(x: 0, y: (UIScreen.main.bounds.size.height - 28) / 2, width: 28, height: 28)
titleContainer.addSubview(searchIcon)
let titleLabel = UILabel()
titleLabel.frame = CGRect(x: 28, y: UIScreen.main.bounds.size.height, width: UIScreen.main.bounds.size.width-28, height: UIScreen.main.bounds.size.height)
titleLabel.textColor = UIColor(red:255, green:255, blue:255, alpha:1.0)
titleContainer.addSubview(self.titleLabel)
self.navigationItem.titleView = titleContainer
problem and expected solution:
Why did you use height of all the element as UIScreen.main.bounds.size.height? Please try the below code.
P.S. I have changed the color of searchIcon as i didn't have the image and changed the color of text of label and added some text so that it is visible.
let titleContainer = UIView()
titleContainer.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 45)
let searchIcon = UIButton()
//searchIcon.setImage(UIImage(named: "search_icon"), for: UIControlState.normal)
searchIcon.backgroundColor = UIColor.red
searchIcon.layer.frame = CGRect(x: 0, y: 8, width: 28, height: 28)
titleContainer.addSubview(searchIcon)
let titleLabel = UILabel()
titleLabel.frame = CGRect(x: 28, y: 8, width: UIScreen.main.bounds.size.width-28, height: 30)
titleLabel.textColor = UIColor.blue//(red:255, green:255, blue:255, alpha:1.0)
titleContainer.addSubview(titleLabel)
titleLabel.text = "Sample Text"
self.navigationItem.titleView = titleContainer
Here is the screenshot of the above code:
Please change the x,y accordingly as per your requirement.
Have you considered making your let bar = UINavigationBar() programmatically and then add it with: self.view.addSubview(bar)?
So you can push all the "UIBarItems" you want..?

Custom titleView changes location after push and pop new screen in stack

I'm trying to implement custom titleView for navigation controller with "title" and "description" labels. If I placed this titleView on first VC, it looks good. If I push second VC into navigation stack and pop it, location of the titleView is changed.
titleView's constraints:
titleLabel.translatesAutoresizingMaskIntoConstraints = false
descriptionLabel.translatesAutoresizingMaskIntoConstraints = false
titleLabel.widthAnchor.constraint(equalTo: widthAnchor).isActive = true
descriptionLabel.widthAnchor.constraint(equalTo: titleLabel.widthAnchor).isActive = true
titleLabel.topAnchor.constraint(equalTo: topAnchor).isActive = true
descriptionLabel.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 2.0).isActive = true
In viewDidLoad of VC I use follow code to insert titleView:
navigationItem.titleView = BarTitleView()
navigationItem.titleView?.bounds = CGRect(x: 0, y: 0, width: view.bounds.width, height: 44)
navigationItem.titleView?.updateConstraints()
I tried to insert follow lines into viewWillAppear (second VC has different bar buttons and it can be root of problem), but nothing changes
navigationItem.titleView?.bounds = CGRect(x: 0, y: 0, width: view.bounds.width, height: 44)
navigationItem.titleView?.updateConstraints()
How can I fixed this issue?
I have created a Title and Subtitle in the Navigation bar without the need for Interface Builder and constraint modification.
My Function to create the title view looks as follows:
class func setTitle(title:String, subtitle:String) -> UIView {
let titleLabel = UILabel(frame: CGRect(x: 0, y: -8, width: 0, height: 0))
titleLabel.backgroundColor = UIColor.clear
titleLabel.textColor = UIColor.white
titleLabel.font = UIFont.boldSystemFont(ofSize: 17)
titleLabel.text = title
titleLabel.sizeToFit()
let subtitleLabel = UILabel(frame: CGRect(x: 0, y: 12, width: 0, height: 0))
subtitleLabel.backgroundColor = UIColor.clear
subtitleLabel.textColor = UIColor.white
subtitleLabel.font = UIFont.systemFont(ofSize: 12)
subtitleLabel.text = subtitle
subtitleLabel.sizeToFit()
// Fix incorrect width bug
if (subtitleLabel.frame.size.width > titleLabel.frame.size.width) {
var titleFrame = titleLabel.frame
titleFrame.size.width = subtitleLabel.frame.size.width
titleLabel.frame = titleFrame
titleLabel.textAlignment = .center
}
let titleView = UIView(frame: CGRect(x: 0, y: 0, width: titleLabel.frame.size.width, height: titleLabel.frame.size.height))
titleView.addSubview(titleLabel)
titleView.addSubview(subtitleLabel)
let widthDiff = subtitleLabel.frame.size.width - titleLabel.frame.size.width
if widthDiff < 0 {
let newX = widthDiff / 2
subtitleLabel.frame.origin.x = abs(newX)
} else {
let newX = widthDiff / 2
titleLabel.frame.origin.x = newX
}
return titleView
}
Then to use the title view in any view controller I just call this line:
self.navigationItem.titleView = Functions.Views.setTitle(title: "Title String", subtitle: "Subtitle String")

Resources