Custom Navigation Title in iOS 12
I am trying to implement a custom Navigation Title on an iOS app.
The StoryBoard looks like this:
The place that I want to have the custom Navigation Title is the last view ( the message view ), and because I use an image and text this means that I need to have custom width and height. By needing this if I do in viewDidLoad
:
let rect = CGRect(x: 0, y:0, width: 150, height: 88)
titleView = UIView(frame: rect)
......
titleView?.addSubview(imageView)
......
titleView?.addSubview(label)
navigationItem.titleView = titleView
The height of the title is blocked to 44pt.
But how I managed to do it is adding the subViews to the navigation bar:
var navigationBar: MessagesNavigationBar? {
guard let navigationBar = navigationController?.navigationBar as? MessagesNavigationBar else {
return nil
}
return navigationBar
}
And in viewDidLoad
let rect = CGRect(x: 0, y:0, width: 150, height: 88)
titleView = UIView(frame: rect)
......
titleView?.addSubview(imageView)
......
titleView?.addSubview(label)
navigationBar?.addSubview(titleView!)
But the problem is that I have to remove the subviews when I leave the view, otherwise whatever I add there will be present in the table view as well.
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if navigationBar != nil {
titleView?.removeFromSuperview()
}
}
Which kinda makes me feel that I'm not doing the right thing and I find difficult to add a fade out animation to those subViews when I leave the conversation. (i.e. native messages app on iOS).
So what is the right way of creating a custom Title Navigation Bar in iOS 12?
Scenes
ios swift xcode uinavigationcontroller uinavigationbar
add a comment |
I am trying to implement a custom Navigation Title on an iOS app.
The StoryBoard looks like this:
The place that I want to have the custom Navigation Title is the last view ( the message view ), and because I use an image and text this means that I need to have custom width and height. By needing this if I do in viewDidLoad
:
let rect = CGRect(x: 0, y:0, width: 150, height: 88)
titleView = UIView(frame: rect)
......
titleView?.addSubview(imageView)
......
titleView?.addSubview(label)
navigationItem.titleView = titleView
The height of the title is blocked to 44pt.
But how I managed to do it is adding the subViews to the navigation bar:
var navigationBar: MessagesNavigationBar? {
guard let navigationBar = navigationController?.navigationBar as? MessagesNavigationBar else {
return nil
}
return navigationBar
}
And in viewDidLoad
let rect = CGRect(x: 0, y:0, width: 150, height: 88)
titleView = UIView(frame: rect)
......
titleView?.addSubview(imageView)
......
titleView?.addSubview(label)
navigationBar?.addSubview(titleView!)
But the problem is that I have to remove the subviews when I leave the view, otherwise whatever I add there will be present in the table view as well.
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if navigationBar != nil {
titleView?.removeFromSuperview()
}
}
Which kinda makes me feel that I'm not doing the right thing and I find difficult to add a fade out animation to those subViews when I leave the conversation. (i.e. native messages app on iOS).
So what is the right way of creating a custom Title Navigation Bar in iOS 12?
Scenes
ios swift xcode uinavigationcontroller uinavigationbar
“By needing this I can't use something like in viewDidLoad” why can’t you? That’s the normal and right thing to do...
– matt
Nov 20 '18 at 22:27
Sorry, I was ambiguous, and I edited the question. So, if I usenavigationItem.titleView = titleView
I cannot change the height of thetitleView
which is by default 44.
– Sebastian Corneliu Vîrlan
Nov 20 '18 at 22:31
add a comment |
I am trying to implement a custom Navigation Title on an iOS app.
The StoryBoard looks like this:
The place that I want to have the custom Navigation Title is the last view ( the message view ), and because I use an image and text this means that I need to have custom width and height. By needing this if I do in viewDidLoad
:
let rect = CGRect(x: 0, y:0, width: 150, height: 88)
titleView = UIView(frame: rect)
......
titleView?.addSubview(imageView)
......
titleView?.addSubview(label)
navigationItem.titleView = titleView
The height of the title is blocked to 44pt.
But how I managed to do it is adding the subViews to the navigation bar:
var navigationBar: MessagesNavigationBar? {
guard let navigationBar = navigationController?.navigationBar as? MessagesNavigationBar else {
return nil
}
return navigationBar
}
And in viewDidLoad
let rect = CGRect(x: 0, y:0, width: 150, height: 88)
titleView = UIView(frame: rect)
......
titleView?.addSubview(imageView)
......
titleView?.addSubview(label)
navigationBar?.addSubview(titleView!)
But the problem is that I have to remove the subviews when I leave the view, otherwise whatever I add there will be present in the table view as well.
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if navigationBar != nil {
titleView?.removeFromSuperview()
}
}
Which kinda makes me feel that I'm not doing the right thing and I find difficult to add a fade out animation to those subViews when I leave the conversation. (i.e. native messages app on iOS).
So what is the right way of creating a custom Title Navigation Bar in iOS 12?
Scenes
ios swift xcode uinavigationcontroller uinavigationbar
I am trying to implement a custom Navigation Title on an iOS app.
The StoryBoard looks like this:
The place that I want to have the custom Navigation Title is the last view ( the message view ), and because I use an image and text this means that I need to have custom width and height. By needing this if I do in viewDidLoad
:
let rect = CGRect(x: 0, y:0, width: 150, height: 88)
titleView = UIView(frame: rect)
......
titleView?.addSubview(imageView)
......
titleView?.addSubview(label)
navigationItem.titleView = titleView
The height of the title is blocked to 44pt.
But how I managed to do it is adding the subViews to the navigation bar:
var navigationBar: MessagesNavigationBar? {
guard let navigationBar = navigationController?.navigationBar as? MessagesNavigationBar else {
return nil
}
return navigationBar
}
And in viewDidLoad
let rect = CGRect(x: 0, y:0, width: 150, height: 88)
titleView = UIView(frame: rect)
......
titleView?.addSubview(imageView)
......
titleView?.addSubview(label)
navigationBar?.addSubview(titleView!)
But the problem is that I have to remove the subviews when I leave the view, otherwise whatever I add there will be present in the table view as well.
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if navigationBar != nil {
titleView?.removeFromSuperview()
}
}
Which kinda makes me feel that I'm not doing the right thing and I find difficult to add a fade out animation to those subViews when I leave the conversation. (i.e. native messages app on iOS).
So what is the right way of creating a custom Title Navigation Bar in iOS 12?
Scenes
ios swift xcode uinavigationcontroller uinavigationbar
ios swift xcode uinavigationcontroller uinavigationbar
edited Nov 20 '18 at 22:42
Sebastian Corneliu Vîrlan
asked Nov 20 '18 at 22:02
Sebastian Corneliu VîrlanSebastian Corneliu Vîrlan
62711128
62711128
“By needing this I can't use something like in viewDidLoad” why can’t you? That’s the normal and right thing to do...
– matt
Nov 20 '18 at 22:27
Sorry, I was ambiguous, and I edited the question. So, if I usenavigationItem.titleView = titleView
I cannot change the height of thetitleView
which is by default 44.
– Sebastian Corneliu Vîrlan
Nov 20 '18 at 22:31
add a comment |
“By needing this I can't use something like in viewDidLoad” why can’t you? That’s the normal and right thing to do...
– matt
Nov 20 '18 at 22:27
Sorry, I was ambiguous, and I edited the question. So, if I usenavigationItem.titleView = titleView
I cannot change the height of thetitleView
which is by default 44.
– Sebastian Corneliu Vîrlan
Nov 20 '18 at 22:31
“By needing this I can't use something like in viewDidLoad” why can’t you? That’s the normal and right thing to do...
– matt
Nov 20 '18 at 22:27
“By needing this I can't use something like in viewDidLoad” why can’t you? That’s the normal and right thing to do...
– matt
Nov 20 '18 at 22:27
Sorry, I was ambiguous, and I edited the question. So, if I use
navigationItem.titleView = titleView
I cannot change the height of the titleView
which is by default 44.– Sebastian Corneliu Vîrlan
Nov 20 '18 at 22:31
Sorry, I was ambiguous, and I edited the question. So, if I use
navigationItem.titleView = titleView
I cannot change the height of the titleView
which is by default 44.– Sebastian Corneliu Vîrlan
Nov 20 '18 at 22:31
add a comment |
2 Answers
2
active
oldest
votes
Creating your custom titleView
and assigning it to navigationItem.titleView
is what you want. On older systems (pre iOS 11) you just might need to call sizeToFit()
on the titleView
.
This way you can create this titleView
override func viewDidLoad() {
super.viewDidLoad()
let imageView = UIImageView()
NSLayoutConstraint.activate([
imageView.heightAnchor.constraint(equalToConstant: 20),
imageView.widthAnchor.constraint(equalToConstant: 20)
])
imageView.backgroundColor = .red
let titleLabel = UILabel()
titleLabel.text = "Custom title"
let hStack = UIStackView(arrangedSubviews: [imageView, titleLabel])
hStack.spacing = 5
hStack.alignment = .center
navigationItem.titleView = hStack
}
You might also need to have the right set of autolayout constraints or use UIStackView
.
sizeToFit()
is not working since iOS 11 as far as I read on other stackoverflow answers, I tried to use that first time as the default height is 44 and I didn't found a way to change it.
– Sebastian Corneliu Vîrlan
Nov 20 '18 at 22:32
Than probably your constraints don't define size of the titleView.
– olejnjak
Nov 20 '18 at 22:36
I don't have any constrains as is just a Navigation Controller -> Table Controller -> View Controller + Navigation Bar. I added to the question a print with the Scenes as well. By the way, thesizeToFit
method is not even called.
– Sebastian Corneliu Vîrlan
Nov 20 '18 at 22:42
ThetitleView
inside need constraints...btw. you were right since iOS 11sizeToFit()
is not needed
– olejnjak
Nov 20 '18 at 22:44
I've edited the original answer so it is a bit clearer
– olejnjak
Nov 20 '18 at 22:48
|
show 2 more comments
These lines have no effect on the size of a title view:
let rect = CGRect(x: 0, y:0, width: 150, height: 88)
titleView = UIView(frame: rect)
Instead (or in addition) give your title view a width constraint and a height constraint. That is how the runtime knows what size you want.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402257%2fcustom-navigation-title-in-ios-12%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Creating your custom titleView
and assigning it to navigationItem.titleView
is what you want. On older systems (pre iOS 11) you just might need to call sizeToFit()
on the titleView
.
This way you can create this titleView
override func viewDidLoad() {
super.viewDidLoad()
let imageView = UIImageView()
NSLayoutConstraint.activate([
imageView.heightAnchor.constraint(equalToConstant: 20),
imageView.widthAnchor.constraint(equalToConstant: 20)
])
imageView.backgroundColor = .red
let titleLabel = UILabel()
titleLabel.text = "Custom title"
let hStack = UIStackView(arrangedSubviews: [imageView, titleLabel])
hStack.spacing = 5
hStack.alignment = .center
navigationItem.titleView = hStack
}
You might also need to have the right set of autolayout constraints or use UIStackView
.
sizeToFit()
is not working since iOS 11 as far as I read on other stackoverflow answers, I tried to use that first time as the default height is 44 and I didn't found a way to change it.
– Sebastian Corneliu Vîrlan
Nov 20 '18 at 22:32
Than probably your constraints don't define size of the titleView.
– olejnjak
Nov 20 '18 at 22:36
I don't have any constrains as is just a Navigation Controller -> Table Controller -> View Controller + Navigation Bar. I added to the question a print with the Scenes as well. By the way, thesizeToFit
method is not even called.
– Sebastian Corneliu Vîrlan
Nov 20 '18 at 22:42
ThetitleView
inside need constraints...btw. you were right since iOS 11sizeToFit()
is not needed
– olejnjak
Nov 20 '18 at 22:44
I've edited the original answer so it is a bit clearer
– olejnjak
Nov 20 '18 at 22:48
|
show 2 more comments
Creating your custom titleView
and assigning it to navigationItem.titleView
is what you want. On older systems (pre iOS 11) you just might need to call sizeToFit()
on the titleView
.
This way you can create this titleView
override func viewDidLoad() {
super.viewDidLoad()
let imageView = UIImageView()
NSLayoutConstraint.activate([
imageView.heightAnchor.constraint(equalToConstant: 20),
imageView.widthAnchor.constraint(equalToConstant: 20)
])
imageView.backgroundColor = .red
let titleLabel = UILabel()
titleLabel.text = "Custom title"
let hStack = UIStackView(arrangedSubviews: [imageView, titleLabel])
hStack.spacing = 5
hStack.alignment = .center
navigationItem.titleView = hStack
}
You might also need to have the right set of autolayout constraints or use UIStackView
.
sizeToFit()
is not working since iOS 11 as far as I read on other stackoverflow answers, I tried to use that first time as the default height is 44 and I didn't found a way to change it.
– Sebastian Corneliu Vîrlan
Nov 20 '18 at 22:32
Than probably your constraints don't define size of the titleView.
– olejnjak
Nov 20 '18 at 22:36
I don't have any constrains as is just a Navigation Controller -> Table Controller -> View Controller + Navigation Bar. I added to the question a print with the Scenes as well. By the way, thesizeToFit
method is not even called.
– Sebastian Corneliu Vîrlan
Nov 20 '18 at 22:42
ThetitleView
inside need constraints...btw. you were right since iOS 11sizeToFit()
is not needed
– olejnjak
Nov 20 '18 at 22:44
I've edited the original answer so it is a bit clearer
– olejnjak
Nov 20 '18 at 22:48
|
show 2 more comments
Creating your custom titleView
and assigning it to navigationItem.titleView
is what you want. On older systems (pre iOS 11) you just might need to call sizeToFit()
on the titleView
.
This way you can create this titleView
override func viewDidLoad() {
super.viewDidLoad()
let imageView = UIImageView()
NSLayoutConstraint.activate([
imageView.heightAnchor.constraint(equalToConstant: 20),
imageView.widthAnchor.constraint(equalToConstant: 20)
])
imageView.backgroundColor = .red
let titleLabel = UILabel()
titleLabel.text = "Custom title"
let hStack = UIStackView(arrangedSubviews: [imageView, titleLabel])
hStack.spacing = 5
hStack.alignment = .center
navigationItem.titleView = hStack
}
You might also need to have the right set of autolayout constraints or use UIStackView
.
Creating your custom titleView
and assigning it to navigationItem.titleView
is what you want. On older systems (pre iOS 11) you just might need to call sizeToFit()
on the titleView
.
This way you can create this titleView
override func viewDidLoad() {
super.viewDidLoad()
let imageView = UIImageView()
NSLayoutConstraint.activate([
imageView.heightAnchor.constraint(equalToConstant: 20),
imageView.widthAnchor.constraint(equalToConstant: 20)
])
imageView.backgroundColor = .red
let titleLabel = UILabel()
titleLabel.text = "Custom title"
let hStack = UIStackView(arrangedSubviews: [imageView, titleLabel])
hStack.spacing = 5
hStack.alignment = .center
navigationItem.titleView = hStack
}
You might also need to have the right set of autolayout constraints or use UIStackView
.
edited Nov 20 '18 at 22:47
answered Nov 20 '18 at 22:31
olejnjakolejnjak
468312
468312
sizeToFit()
is not working since iOS 11 as far as I read on other stackoverflow answers, I tried to use that first time as the default height is 44 and I didn't found a way to change it.
– Sebastian Corneliu Vîrlan
Nov 20 '18 at 22:32
Than probably your constraints don't define size of the titleView.
– olejnjak
Nov 20 '18 at 22:36
I don't have any constrains as is just a Navigation Controller -> Table Controller -> View Controller + Navigation Bar. I added to the question a print with the Scenes as well. By the way, thesizeToFit
method is not even called.
– Sebastian Corneliu Vîrlan
Nov 20 '18 at 22:42
ThetitleView
inside need constraints...btw. you were right since iOS 11sizeToFit()
is not needed
– olejnjak
Nov 20 '18 at 22:44
I've edited the original answer so it is a bit clearer
– olejnjak
Nov 20 '18 at 22:48
|
show 2 more comments
sizeToFit()
is not working since iOS 11 as far as I read on other stackoverflow answers, I tried to use that first time as the default height is 44 and I didn't found a way to change it.
– Sebastian Corneliu Vîrlan
Nov 20 '18 at 22:32
Than probably your constraints don't define size of the titleView.
– olejnjak
Nov 20 '18 at 22:36
I don't have any constrains as is just a Navigation Controller -> Table Controller -> View Controller + Navigation Bar. I added to the question a print with the Scenes as well. By the way, thesizeToFit
method is not even called.
– Sebastian Corneliu Vîrlan
Nov 20 '18 at 22:42
ThetitleView
inside need constraints...btw. you were right since iOS 11sizeToFit()
is not needed
– olejnjak
Nov 20 '18 at 22:44
I've edited the original answer so it is a bit clearer
– olejnjak
Nov 20 '18 at 22:48
sizeToFit()
is not working since iOS 11 as far as I read on other stackoverflow answers, I tried to use that first time as the default height is 44 and I didn't found a way to change it.– Sebastian Corneliu Vîrlan
Nov 20 '18 at 22:32
sizeToFit()
is not working since iOS 11 as far as I read on other stackoverflow answers, I tried to use that first time as the default height is 44 and I didn't found a way to change it.– Sebastian Corneliu Vîrlan
Nov 20 '18 at 22:32
Than probably your constraints don't define size of the titleView.
– olejnjak
Nov 20 '18 at 22:36
Than probably your constraints don't define size of the titleView.
– olejnjak
Nov 20 '18 at 22:36
I don't have any constrains as is just a Navigation Controller -> Table Controller -> View Controller + Navigation Bar. I added to the question a print with the Scenes as well. By the way, the
sizeToFit
method is not even called.– Sebastian Corneliu Vîrlan
Nov 20 '18 at 22:42
I don't have any constrains as is just a Navigation Controller -> Table Controller -> View Controller + Navigation Bar. I added to the question a print with the Scenes as well. By the way, the
sizeToFit
method is not even called.– Sebastian Corneliu Vîrlan
Nov 20 '18 at 22:42
The
titleView
inside need constraints...btw. you were right since iOS 11 sizeToFit()
is not needed– olejnjak
Nov 20 '18 at 22:44
The
titleView
inside need constraints...btw. you were right since iOS 11 sizeToFit()
is not needed– olejnjak
Nov 20 '18 at 22:44
I've edited the original answer so it is a bit clearer
– olejnjak
Nov 20 '18 at 22:48
I've edited the original answer so it is a bit clearer
– olejnjak
Nov 20 '18 at 22:48
|
show 2 more comments
These lines have no effect on the size of a title view:
let rect = CGRect(x: 0, y:0, width: 150, height: 88)
titleView = UIView(frame: rect)
Instead (or in addition) give your title view a width constraint and a height constraint. That is how the runtime knows what size you want.
add a comment |
These lines have no effect on the size of a title view:
let rect = CGRect(x: 0, y:0, width: 150, height: 88)
titleView = UIView(frame: rect)
Instead (or in addition) give your title view a width constraint and a height constraint. That is how the runtime knows what size you want.
add a comment |
These lines have no effect on the size of a title view:
let rect = CGRect(x: 0, y:0, width: 150, height: 88)
titleView = UIView(frame: rect)
Instead (or in addition) give your title view a width constraint and a height constraint. That is how the runtime knows what size you want.
These lines have no effect on the size of a title view:
let rect = CGRect(x: 0, y:0, width: 150, height: 88)
titleView = UIView(frame: rect)
Instead (or in addition) give your title view a width constraint and a height constraint. That is how the runtime knows what size you want.
answered Nov 20 '18 at 22:45
mattmatt
329k46534732
329k46534732
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402257%2fcustom-navigation-title-in-ios-12%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
“By needing this I can't use something like in viewDidLoad” why can’t you? That’s the normal and right thing to do...
– matt
Nov 20 '18 at 22:27
Sorry, I was ambiguous, and I edited the question. So, if I use
navigationItem.titleView = titleView
I cannot change the height of thetitleView
which is by default 44.– Sebastian Corneliu Vîrlan
Nov 20 '18 at 22:31