Remove Gradient and make Transparent navigation bar in ios 15 - swift

I have tried the following code to remove the gradient from navigation and make a transparent navigation bar for ios 15, But it didn't work on the same whereas in the lower version it works perfectly fine.
if let bar = self.navigationController?.navigationBar {
if #available(iOS 15.0, *) {
let navigationBarAppearance = UINavigationBarAppearance()
navigationBarAppearance.configureWithTransparentBackground()
navigationBarAppearance.shadowImage = UIImage()
self.title = ""
navigationBarAppearance.backgroundImage = UIImage()
navigationBarAppearance.backgroundColor = .clear
UINavigationBar.appearance().isTranslucent = true
UINavigationBar.appearance().standardAppearance = navigationBarAppearance
UINavigationBar.appearance().scrollEdgeAppearance = navigationBarAppearance
} else {
bar.setBackgroundImage(FCUtil.getImageWithColor(color: UIColor.clear, size: bar.frame.size), for: UIBarMetrics.default)
bar.isTranslucent = true
bar.shadowImage = UIImage()
self.title = ""
}
}
Please suggest to me, If I had missed something.

The only problem with your code is the timing. You are running the code too late. Changing the navigation bar appearance() cannot change a navigation bar that already exists.
Instead, just change bar itself, directly:
bar.standardAppearance = navigationBarAppearance
bar.scrollEdgeAppearance = navigationBarAppearance

Related

Apply different custom UINavigationBar styles for multiple Views in SwiftUI

I am currently struggling with defining multiple custom UINavigationBars for different Views in SwiftUI.
I have defined two different appearances in my UINavigationBar extension: a default one and one with a transparent background.
I am calling UINavigationBar.defaultNavigationBar() in my entry ContentView().
And when I'm navigating to another view via NavigationLink { ... }, I want to apply the different navigation bar appearances based on the logic in setNavigationBarStyle
The switch does not work and the UINavigationBar keeps its initial configuration which is defined in defaultNavigationBar()
So my question is, is this the right approach and I am missing some configurations? If not, what is the right approach to style the UINavigationBar differently based on my current view?
import SwiftUI
extension UINavigationBar {
static let appearance = UINavigationBarAppearance()
static func defaultNavigationBar() {
appearance.backgroundColor = UIColor(Color.audi.barBackground)
appearance.titleTextAttributes = [
NSAttributedString.Key.foregroundColor: UIColor(Color.audi.barContent),
NSAttributedString.Key.font: UIFont(name: Fonts.typeVFNormalBold.rawValue, size: 12)!
]
appearance.shadowColor = UIColor(Color.audi.barTopSeparator)
appearance.setBackIndicatorImage(UIImage(named: "back-small")!, transitionMaskImage: UIImage(named: "back-small")!)
UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().compactAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = appearance
}
static func transparentNavigationBar() {
appearance.configureWithTransparentBackground()
appearance.titleTextAttributes = [
NSAttributedString.Key.foregroundColor: UIColor(Color.audi.barContentOverlay),
NSAttributedString.Key.font: UIFont(name: Fonts.typeVFNormalBold.rawValue, size: 12)!
]
appearance.shadowColor = .clear
appearance.setBackIndicatorImage(UIImage(named: "back-white-small")!, transitionMaskImage: UIImage(named: "back-white-small")!)
UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().compactAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = appearance
}
}
func setNavigationBarStyle(scrollY: CGFloat) {
if scrollY <= 0 {
UINavigationBar.transparentNavigationBar()
} else {
UINavigationBar.defaultNavigationBar()
}
}

AdjustsFontSizeToFitWidth equivalent for iOS15 UIButton

I'm using UIKit, not SwiftUI. Previously, I used setAttributedTitle and I also set AdjustsFontSizeToFitWidth to true, and the font size shrinks to match the button width. I can't seem to match this behavior with the new button configurations and swift's AttributedString.
Here's what I have:
var config = UIButton.Configuration.filled()
config.imagePadding = 5
config.imagePlacement = .trailing
config.image = UIImage(systemName: "ellipsis.circle",
withConfiguration: UIImage.SymbolConfiguration(scale: .medium))
config.titlePadding = 10
config.cornerStyle = .capsule
let handler: UIButton.ConfigurationUpdateHandler = { button in
button.configuration?.background.backgroundColor = Colors.specLabel
button.configuration?.baseForegroundColor = Colors.greyText
}
myButton.configuration = config
myButton.configurationUpdateHandler = handler
And then
guard let attributedString = try? AttributedString(markdown: "hello **hello** hello") else { return }
myButton.configuration?.attributedTitle = attributedString
Every time, I get a multi-line button. Setting numberOfLines, lineBreakMode and adjustsFontSizeToFitWidth is all ignored. Any ideas?

Making Cocoa NSWindow translucent hides storyboard elements

In my app delegate I make my window translucent with the following code:
func applicationDidFinishLaunching(_ aNotification: Notification) {
let visualEffect = NSVisualEffectView()
visualEffect.translatesAutoresizingMaskIntoConstraints = false
visualEffect.material = .dark
visualEffect.state = .active
visualEffect.wantsLayer = true
visualEffect.layer?.cornerRadius = 16.0
NSApplication.shared.mainWindow?.titleVisibility = .hidden
NSApplication.shared.mainWindow?.styleMask.remove(.titled)
NSApplication.shared.mainWindow?.backgroundColor = .clear
NSApplication.shared.mainWindow?.isMovableByWindowBackground = true
NSApplication.shared.mainWindow?.contentView?.addSubview(visualEffect)
guard let constraints = NSApplication.shared.mainWindow?.contentView else {
return
}
visualEffect.leadingAnchor.constraint(equalTo: constraints.leadingAnchor).isActive = true
visualEffect.trailingAnchor.constraint(equalTo: constraints.trailingAnchor).isActive = true
visualEffect.topAnchor.constraint(equalTo: constraints.topAnchor).isActive = true
visualEffect.bottomAnchor.constraint(equalTo: constraints.bottomAnchor).isActive = true
}
The problem with this is that every element in the storyboard is no longer visible. How can I fix this? Thanks
You need to add all your UI to the NSVisualEffectView as subviews or move the NSVisualEffectView to the back of the view hierarchy:
NSApplication.shared.mainWindow?.contentView?.addSubview(visualEffect, positioned: .below, relativeTo: nil)
Update:
I created a new macOS project in Xcode and added a label on the view. Then I pasted your code and the only thing I changed was the line of code above. It works.

Curve search bar Swift 4 [duplicate]

This question already has answers here:
UISearchBar custom corners
(5 answers)
Closed 4 years ago.
i had problem to change my search bar design programatically on navigation controller. i just want to make the search bar curve like image below:
i had try a lot of example in stack overflow, but i still failed to change the shape. Here what it looks like now:
and here is the source code, on what have already done:
//MARK for searchBar UI
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.placeholder = " Search for programs..."
searchController.searchBar.sizeToFit()
searchController.searchBar.isTranslucent = false
searchController.searchBar.backgroundImage = UIImage()
searchController.searchBar.delegate = self
navigationItem.titleView = searchController.searchBar
self.searchController.searchBar.layer.cornerRadius = 20.0
self.searchController.searchBar.clipsToBounds = true
// SearchBar text
let textFieldInsideUISearchBar = searchController.searchBar.value(forKey: "searchField") as? UITextField
textFieldInsideUISearchBar?.borderStyle = .roundedRect
textFieldInsideUISearchBar?.font = textFieldInsideUISearchBar?.font?.withSize(14)
and now, i really have no idea how to change it, thanks in advance.
This will help you,
let searchBar = UISearchBar()
On viewDidLoad()
searchBar.sizeToFit()
searchBar.placeholder = "Search"
self.navigationController?.navigationBar.barTintColor = UIColor.green //this is just for reference
self.navigationItem.titleView = searchBar
if let textfield = searchBar.value(forKey: "searchField") as? UITextField {
textfield.textColor = UIColor.blue
if let backgroundview = textfield.subviews.first {
backgroundview.backgroundColor = UIColor.blue
backgroundview.layer.cornerRadius = 20;
backgroundview.clipsToBounds = true;
}
}
Above code will give you following output,
increasing the corner radius should work

MFMailComposeViewController navigationBar custom background color

I'm using MFMailComposeViewController and I'd like to change background color so it matches the one I have across the app. I've tried several things, but nothing worked(at least not on iOS 9).
let mailVC = MFMailComposeViewController()
mailVC.mailComposeDelegate = self
...
mailVC.navigationBar.titleTextAttributes =
[NSForegroundColorAttributeName: UIColor.whiteColor()] // this works
mailVC.navigationBar.tintColor = UIColor.whiteColor() // this works
mailVC.navigationBar.barTintColor = UIColor.blueColor() // this doesn't work
mailVC.navigationBar.backgroundColor = UIColor.blueColor() // this doesn't work
Background color stays default gray.
I solved it by setting color of navigationbar before initializing MFMailComposeViewController like this:
UINavigationBar.appearance().barTintColor = UIColor.blueColor()
let mailVC = MFMailComposeViewController()