Change color of done button in WKWebView - ios

I use this code in appDelegate to change color of all UIBarButtonItem of my navigationBar in my project to white
let whiteAttr = [NSAttributedStringKey.font : UIFont(name: "OpenSans", size: 14)! ,NSAttributedStringKey.foregroundColor: UIColor.white]
UIBarButtonItem.appearance().setTitleTextAttributes(whiteAttr, for: .normal)
UIBarButtonItem.appearance().setTitleTextAttributes(whiteAttr, for: .highlighted)
But Done button of my ToolBar in WKWebView is also white, how can i change the color ?

Just got this working using the appearance APIs from iOS 13:
let doneButtonAppearance = UIBarButtonItemAppearance()
doneButtonAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.blue]
let toolBarAppearance = UIToolbarAppearance()
toolBarAppearance.doneButtonAppearance = doneButtonAppearance
UIToolbar.appearance().standardAppearance = toolBarAppearance
// Unsure if this is needed
if #available(iOS 15.0, *) {
UIToolbar.appearance().scrollEdgeAppearance = toolBarAppearance
}

let BarButtonItemAppearance = UIBarButtonItem.appearance()
BarButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.blue], for: .normal)

Related

iPadOS 15 UITabBar title cut off

Since I upgraded my iPad operating system, the title of the UITabBar of my app is showing truncated, as shown in the screenshot.
I have tried some methods, but I have not found the correct solution.
Hope someone can help me.
And Here is code:
func setupTabBar() {
if #available(iOS 13, *) {
let appearance = tabBar.standardAppearance
appearance.configureWithOpaqueBackground()
appearance.backgroundImage = UIImage(color: .white)
appearance.shadowImage = UIImage(color: .clear)
let normalAttrs: [NSAttributedString.Key: Any] = [.foregroundColor: ThemeColor.gray]
let selectedAttrs: [NSAttributedString.Key: Any] = [.foregroundColor: ThemeColor.red]
appearance.stackedLayoutAppearance.selected.titleTextAttributes = selectedAttrs
appearance.stackedLayoutAppearance.normal.titleTextAttributes = normalAttrs
appearance.inlineLayoutAppearance.selected.titleTextAttributes = selectedAttrs
appearance.inlineLayoutAppearance.normal.titleTextAttributes = normalAttrs
appearance.compactInlineLayoutAppearance.selected.titleTextAttributes = selectedAttrs
appearance.compactInlineLayoutAppearance.normal.titleTextAttributes = normalAttrs
UITabBar.appearance().standardAppearance = appearance
} else {
tabBar.backgroundImage = UIImage(color: .white)
tabBar.shadowImage = UIImage(color: .clear)
}
if #available(iOS 15, *) {
UITabBar.appearance().scrollEdgeAppearance = UITabBar.appearance().standardAppearance
}
}
For some reason, it seems that setting the titleTextAttributes is what causes the problem to happen with inlineLayoutAppearance, and including the default paragraph style of NSParagraphStyle.default fixes it.
For your code, the following changes should fix it (as of iOS 15.0).
let normalAttrs: [NSAttributedString.Key: Any] = [.foregroundColor: ThemeColor.gray, .paragraphStyle: NSParagraphStyle.default]
let selectedAttrs: [NSAttributedString.Key: Any] = [.foregroundColor: ThemeColor.red, .paragraphStyle: NSParagraphStyle.default]
For those that cannot have a default paragraph style, setting the attributes for most States got the OS to size the labels correctly for me.
Swift 5.0:
UITabBarItem.appearance()
.setTitleTextAttributes(
customAttributesWithCustomParagraphStyle,
for: [
.normal,
.highlighted,
.disabled,
.selected,
.focused,
.application,
]
)
Without setting it for at least .normal and .selected, the UITabBarItem title gets truncated when used with custom attributes.

Navigation Bar title font problem on ios 13

I’m using Xcode 11.4 and iOS 13.4.
I have set navigation bar title custom font using UINavigatinBar.appearance()
And it works correctly but on iOS 13+ when i try to push to another VC and then comeback to the parent VC, the parent VC title font suddenly has been set to default font and after a second it changes back to the custom font.
Below is a gif of the problem:
nav bar font problem
iOS 13.+ has UINavigationBarAppearance approach to customize NavigationBar-Title & NavigationBar-BarButtonItems
Check this code, might help you
let titleFontAttrs = [ NSAttributedString.Key.font: UIFont(name: "custom-font-name", size: 20)!, NSAttributedString.Key.foregroundColor: UIColor.white ]
let barButtonFontAttrs = [ NSAttributedString.Key.font: UIFont(name: "custom-font-name", size: 14)! ]
UINavigationBar.appearance().tintColor = UIColor.white // bar icons
if #available(iOS 13.0, *) {
let appearance = UINavigationBarAppearance()
appearance.backgroundColor = .red // If you want different nav background color other than white
appearance.titleTextAttributes = titleFontAttrs
appearance.largeTitleTextAttributes = titleFontAttrs // If your app supports largeNavBarTitle
UINavigationBar.appearance().isTranslucent = false
appearance.buttonAppearance.normal.titleTextAttributes = barButtonFontAttrs
appearance.buttonAppearance.highlighted.titleTextAttributes = barButtonFontAttrs
UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().compactAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = appearance
} else {
UINavigationBar.appearance().barTintColor = .red // bar background
UINavigationBar.appearance().titleTextAttributes = titleFontAttrs
UINavigationBar.appearance().isTranslucent = false
UIBarButtonItem.appearance().setTitleTextAttributes(barButtonFontAttrs, for: .normal)
UIBarButtonItem.appearance().setTitleTextAttributes(barButtonFontAttrs, for: .highlighted)
}
Here you go, manage it in viewDidAppear:
let lblTitle = UILabel()
let titleAttribute: [NSAttributedString.Key: Any] = [.font: UIFont.boldSystemFont(ofSize: 21),
.foregroundColor: UIColor.black]
let attributeString = NSMutableAttributedString(string: "Navigation Title", attributes: titleAttribute)
lblTitle.attributedText = attributeString
lblTitle.sizeToFit()
navigationItem.titleView = lblTitle

Change title color of navigation bar in MFMailComposeViewController in iOS 12 not working [duplicate]

This question already has an answer here:
iOS 12.0 : Is there a way to set MFMailComposeViewController navigation bar title's text to white?
(1 answer)
Closed 1 year ago.
How can I change the title color of UINavigationBar in MFMailComposeViewController in iOS 12?
This is what I am doing:
import MessageUI
extension MFMailComposeViewController {
open override func viewDidLoad() {
super.viewDidLoad()
navigationBar.isTranslucent = false
navigationBar.isOpaque = false
navigationBar.barTintColor = .white
navigationBar.tintColor = .white
navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white]
}
}
In iOS 10 works:
In iOS 11 works:
In iOS 12 is not working:
/// UINavigationBar
let navBarAppearance = UINavigationBar.appearance()
navBarAppearance.barTintColor = .red
navBarAppearance.tintColor = .white
navBarAppearance.backgroundColor = .red
navBarAppearance.titleTextAttributes = [
.foregroundColor: UIColor.white,
.font: UIFont.systemFontSize
]
navBarAppearance.largeTitleTextAttributes = [
.foregroundColor: UIColor.white,
.font: UIFont.smallSystemFontSize
]
let barButtonAppearance = UIBarButtonItem.appearance()
barButtonAppearance.setTitleTextAttributes([.font: UIFont.systemFontSize], for: .normal)
navBarAppearance.isTranslucent = false
if #available(iOS 13.0, *) {
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = navBarAppearance.backgroundColor
appearance.titleTextAttributes = navBarAppearance.titleTextAttributes ?? [:]
appearance.largeTitleTextAttributes = navBarAppearance.largeTitleTextAttributes ?? [:]
navBarAppearance.standardAppearance = appearance
navBarAppearance.scrollEdgeAppearance = appearance
}
I tried all the way to change the title color, however it doesn't work
Before presenting the mailcomopser controller
I changed the background color to white
and buttons color to black
Here is the code below:
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: UIBarPosition.any, barMetrics: UIBarMetrics.default)
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().barTintColor = UIColor.white
UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().clipsToBounds = false
UINavigationBar.appearance().backgroundColor = UIColor.white
UIBarButtonItem.appearance().setTitleTextAttributes([.foregroundColor: UIColor.black], for: .normal)
UIBarButtonItem.appearance().setTitleTextAttributes([.foregroundColor: UIColor.black], for: .highlighted)
UIBarButtonItem.appearance().setTitleTextAttributes([.foregroundColor: UIColor.clear], for: .disabled)
UIBarButtonItem.appearance().setTitleTextAttributes([.foregroundColor: UIColor.black], for: .selected)
In your AppDelegate.swift file in the didFinishLaunchingWithOptions launchOptions block
TRY THIS:
let navigationBarAppearace = UINavigationBar.appearance()
navigationBarAppearace.barTintColor = .blue //your desired color
navigationBarAppearace.tintColor = .white //your button etc color
navigationBarAppearace.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white] //your Title Text color
titleView still works fine. Just create view with label in story board like this, with such font, as U need and set it as title view.
if let view = Bundle.main.loadNibNamed("WTitleView", owner: self, options: nil)?.first as? UIView {
navigationItem.titleView = view
}

Swift navigation bar back button frame breaks

I changed font of navigation bar title and back button in AppDelegate following way
private func setupNavigationBar() {
//Navigation Bar
let attrs: [NSAttributedStringKey: Any] = [
NSAttributedStringKey.foregroundColor: UIColor.white,
NSAttributedStringKey.font: FontFamily.Muller.medium.font(size: 17)
]
UIApplication.shared.statusBarStyle = .lightContent
UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().backgroundColor = ColorName.mainBlue.color
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().titleTextAttributes = attrs
let barButtonItemAppearance = UIBarButtonItem.appearance()
barButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.font: FontFamily.Muller.regular.font(size: 15)], for: .normal)
barButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.font:
FontFamily.Muller.regular.font(size: 15)], for: .highlighted)
}
And it's ok
But frame of back button breaks on 3rd screen.
Any ideas how I can fix this?

UISearchbar add navigationController?.navigationBar,How to change "Cancel" color

UISearchbar add navigationController?.navigationBar. How to change "Cancel" color
_searchBar = UISearchBar(frame: CGRect(frame: CGRect(x: 0, y: 0, width: 414, height: 64)))
_searchBar!.delegate = self
_searchBar!.showsCancelButton = true
backgroundColor = .white
barTintColor = .white
tintColor = .red
backgroundImage = UIImage()
_searchBar!.placeholder = "搜索文件"
navigationController?.navigationBar.addSubview(_searchBar!)
This result is "Cancel" color is white. How do I set it to red?
Use appearance function of UIAppearance module -
Method 1:- Visible cancel button color when searchBar on load-
let attributes = [NSForegroundColorAttributeName : UIColor.red]
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(attributes, for: .normal)
or -
Method 2:-
Visible cancel button color after searchBar clicked -
UIBarButtonItem.appearance(whenContainedInInstancesOf:[UISearchBar.self]).tintColor = UIColor.red
Changed the format to readability
let cancelButtonAttributes: NSDictionary = [NSForegroundColorAttributeName: UIColor.YOUR COLOR] UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes as? [String : AnyObject], forState: UIControlState.Normal)
let cancelButtonAttributes: NSDictionary = [NSForegroundColorAttributeName: ``UIColor.YOUR COLOR]
UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes as? [String : AnyObject], forState: UIControlState.Normal)

Resources