I am trying to show a large Title
in a Navigation bar
, but with clear background. When scrolled up, it will be the Navigation bar
with a blur effect.
This looks correct, however, when scrolling, the animation seems to be broken. Also, transition gets stuck from time to time:
My code as follows:
UINavigationController
:
override func viewDidLoad() {
super.viewDidLoad()
if #available(iOS 13.0, *) {
self.navigationBar.prefersLargeTitles = true
let style = UINavigationBarAppearance()
style.configureWithDefaultBackground()
style.titleTextAttributes = [.font: UIFont.systemFont(ofSize: 18)]
self.navigationBar.standardAppearance = style
self.navigationBar.compactAppearance = style
//Configure Large Style
let largeStyle = UINavigationBarAppearance()
largeStyle.configureWithTransparentBackground()
largeStyle.largeTitleTextAttributes = [.font: UIFont.systemFont(ofSize: 28)]
self.navigationBar.scrollEdgeAppearance = largeStyle
}
}
The UITableView
is inside the UINavigationController
. Both are from storyboards via a segue way.
Instead of UITableview, You can try using UITableViewController. I have tried using UITableViewController and its working fine for me. Please check the following design and code.
class ViewController: UITableViewController
{
override func viewDidLoad() {
super.viewDidLoad()
if #available(iOS 13.0, *) {
self.navigationController?.navigationBar.prefersLargeTitles = true
let style = UINavigationBarAppearance()
style.configureWithDefaultBackground()
style.titleTextAttributes = [.font: UIFont.systemFont(ofSize: 18)]
self.navigationController?.navigationBar.standardAppearance = style
self.navigationController?.navigationBar.compactAppearance = style
//Configure Large Style
let largeStyle = UINavigationBarAppearance()
largeStyle.configureWithTransparentBackground()
largeStyle.largeTitleTextAttributes = [.font: UIFont.systemFont(ofSize: 28)]
self.navigationController?.navigationBar.scrollEdgeAppearance = largeStyle
}
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
// Do any additional setup after loading the view.
}
override func numberOfSections(in tableView: UITableView) -> Int {
1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
10
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.textLabel?.text = "\(indexPath.row)"
return cell
}
}
Output:-
hey How are you here is your code try to remove this line
largeStyle.configureWithTransparentBackground()
you have to configure with white background
your code:
override func viewDidLoad() {
super.viewDidLoad()
if #available(iOS 13.0, *) {
self.navigationBar.prefersLargeTitles = true
let style = UINavigationBarAppearance()
style.configureWithDefaultBackground()
style.titleTextAttributes = [.font: UIFont.systemFont(ofSize: 18)]
self.navigationBar.standardAppearance = style
self.navigationBar.compactAppearance = style
//Configure Large Style
let largeStyle = UINavigationBarAppearance()
largeStyle.configureWithTransparentBackground()
largeStyle.largeTitleTextAttributes = [.font: UIFont.systemFont(ofSize: 28)]
self.navigationBar.scrollEdgeAppearance = largeStyle
}
}
Debug - 1) In view debugger, check if navigation title goes beyond navigation bar bounds.
If this is case then
let titleHeight = UIFont.systemFont(ofSize: 28).lineHeight
if titleHeight > self.view.frame.size.height {
self.navigationController.navigationBar.frame = CGRectMake(0, 0, self.view.frame.size.width, titleHeight + topAndBottomPadding)
}
changing properties via storyboard night i've some insights. usually when i'm stuck i reflect same changes that i made programmatically to storyboard and the things left in code are usually the ones causing my bug. try this out if possible
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.