How to remove title from UIBarButtonItem.appearance() on iOS 10.3 - swift

I want to personalize the Back Button of my app to have a consistent looking. So I'm setting a image as the Back Button of a Navigation Bar.
The code bellow works fine on any iOS above iOS 10. So, I'm trying to make it work on iOS 10.3.1.
This is basically all the code, that I'm setting on AppDelegate didFinishLaunchingWithOptions
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
applyNavigationAppearances()
return true
}
private func applyNavigationAppearances() {
let navigationAppearance = UINavigationBar.appearance()
navigationAppearance.barTintColor = .white
navigationAppearance.barStyle = .black
navigationAppearance.backIndicatorImage = UIImage()
navigationAppearance.backIndicatorTransitionMaskImage = UIImage()
navigationAppearance.tintColor = .gray
navigationAppearance.titleTextAttributes = [
NSAttributedString.Key.foregroundColor: UIColor.gray,
NSAttributedString.Key.font: UIFont(name: "Helvetica-Bold", size: 20)
]
let backImage = UIImage(named: "chevron-orange-left")
let backImageUIEdgeInsets = UIEdgeInsets(top: -4, left: -8, bottom: -2, right: 8)
let backImageWithAlignmentRectInsets = backImage?.withAlignmentRectInsets(backImageUIEdgeInsets)
let barButtonAppearance = UIBarButtonItem.appearance()
barButtonAppearance.setBackButtonBackgroundVerticalPositionAdjustment(-1.0, for: .default)
barButtonAppearance.setBackButtonBackgroundImage(backImageWithAlignmentRectInsets, for: .normal, barMetrics: .default)
}
This is what I want and get on any iOS above 10:
This is what I get on iOS 10.3.1:
Edit 1: It's worth saying this is a big app. A solution that didn't involved changing every ViewController would be ideal.
Edit 2: Thank you for answer guys. However, all solutions posted here are distorting the image shorter or longer, so they don't quite solve my problem.

You have:
let backImage = UIImage(named: "icon")
let backImageUIEdgeInsets = UIEdgeInsets(top: -4, left: -8, bottom: -2, right: 8)
let backImageWithAlignmentRectInsets = backImage?.withAlignmentRectInsets(backImageUIEdgeInsets)
appearance.setBackButtonBackgroundVerticalPositionAdjustment(-1.0, for: .default)
appearance.setBackButtonBackgroundImage(backImageWithAlignmentRectInsets,
for: .normal,
barMetrics: .default)
After that you can add :
appearance.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.clear],
for: .normal)
appearance.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.clear],
for: .highlighted)
I check this on iPhone 7 10.3.1

Related

PHPicker can't load selected image to UIButton

I created a UIButton programmatically Which can pick profile photo.
So I made image picker with PHPicker, and it shows well.
But after I selected image, it doesn't show on the button.
I thought the original photos could be the problem. But it's the same with the newly downloaded photo except it sometimes shows white background or send real error phrase.
There is some warning on console like "Xxx(file directory)’ couldn’t be opened because there is no such file"
This PHPicker code worked well before, so I can't assume what is the problem.
How can I select photo well?
private let plusPhotoButton: UIButton = {
let button = UIButton(type: .system)
button.setImage(UIImage(named: "plus_photo"), for: .normal)
button.tintColor = .white
button.addTarget(self, action: #selector(imagePickerTapped), for: .touchUpInside)
return button
}()
#objc func imagePickerTapped() {
print("imagePickerTapped")
var configuration = PHPickerConfiguration()
configuration.selectionLimit = 1
let picker = PHPickerViewController(configuration: configuration)
picker.delegate = self
self.present(picker, animated: true, completion: nil)
}
// MARK: - PHPicker extention
extension RegistrationController : PHPickerViewControllerDelegate {
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
picker.dismiss(animated: true)
let itemProvider = results.first?.itemProvider
if let itemProvider = itemProvider, itemProvider.canLoadObject(ofClass: UIImage.self) {
itemProvider.loadObject(ofClass: UIImage.self) { (image, error) in
DispatchQueue.main.async {
self.plusPhotoButton.layer.cornerRadius = self.plusPhotoButton.frame.width / 2
self.plusPhotoButton.layer.masksToBounds = true
self.plusPhotoButton.layer.borderColor = UIColor.black.cgColor
self.plusPhotoButton.layer.borderWidth = 2
self.plusPhotoButton.setImage(image as? UIImage, for: .normal)
}
}
} else {
print("Image wasn't loaded!!!!")
}
}
}
I added frame setting for this button below
view.addSubview(plusPhotoButton)
plusPhotoButton.centerX(inView: view)
plusPhotoButton.setDimensions(height: 140, width: 140)
plusPhotoButton.anchor(top: view.safeAreaLayoutGuide.topAnchor, paddingTop: 32)
let stack = UIStackView(arrangedSubviews: [emailTextField, passwordTextField, fullnameTextField, UsernameTextField, loginButton])
stack.axis = .vertical
stack.spacing = 20
view.addSubview(stack)
stack.anchor(top: plusPhotoButton.bottomAnchor, left: view.leftAnchor, right: view.rightAnchor, paddingTop: 32, paddingLeft: 32, paddingRight: 32)
'''
After reviewing your code you need make below changes in your code:
Replace
self.plusPhotoButton.setImage(image as? UIImage, for: .normal)
with
self.plusPhotoButton.setBackgroundImage(image as? UIImage, for: .normal)
And you also need to remove image from button with:
self.plusPhotoButton.setImage(nil, for: .normal)
And here is your code with minor improvement:
guard let img = image as? UIImage else { return }
self.profileImage = img
self.plusPhotoButton.layer.cornerRadius = self.plusPhotoButton.frame.width / 2
self.plusPhotoButton.layer.masksToBounds = true
self.plusPhotoButton.layer.borderColor = UIColor.black.cgColor
self.plusPhotoButton.layer.borderWidth = 2
self.plusPhotoButton.setBackgroundImage(img, for: .normal)
self.plusPhotoButton.setImage(nil, for: .normal)

UINavigation custom back button image not working in iOS 15

I want to apply UINavigation custom background image and back button image also for iOS 14 everything is working fine but as I try to run the app on iOS 15 back button image not working instead it's showing the default back button which I want to replace with custom back button image I am using below code
if #available(iOS 15.0, *) {
let appearance = UINavigationBarAppearance()
appearance.configureWithTransparentBackground()
appearance.backgroundImage = image
appearance.setBackIndicatorImage(backButtonImage, transitionMaskImage: backButtonImage)
if Locale.current.languageCode == "fr" {
appearance.titleTextAttributes = [ NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 14), NSAttributedString.Key.foregroundColor: UIColor(hexString: "#231F20")]
} else {
appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor(hexString: "#231F20")]
}
navigationItem.standardAppearance = appearance
navigationItem.scrollEdgeAppearance = appearance
} else {
self.navigationController?.navigationBar.setBackgroundImage(image, for: .default)
self.navigationController?.navigationBar.backIndicatorImage = backButtonImage
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = backButtonImage
if Locale.current.languageCode == "fr" {
self.navigationController?.navigationBar.titleTextAttributes = [ NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 14), NSAttributedString.Key.foregroundColor: UIColor(hexString: "#231F20")]
} else {
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor(hexString: "#231F20")]
}
}
Sharing with you a common function I have made in a BaseViewController which is the superclass of all my View Controllers.
I Simply call the addBackButton function from viewDidLoad from any of the VC I want to add the back button.
It also handles the back navigation automatically. (takes care if you have presented or pushed a vc)
func addBackButton(tint : UIColor? = nil, backImage : UIImage? = Constants.Images.kImageForBackNavigation){
let buttonForBack = UIButton(type: .custom)
var tintColorForImage = UIColor.white
if tint != nil {
tintColorForImage = tint!
}
let image = backImage!.withRenderingMode(.alwaysTemplate)
buttonForBack.setImage(image, for: .normal)
buttonForBack.imageView?.tintColor = tintColorForImage
buttonForBack.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
buttonForBack.addTarget(self, action: #selector(BaseViewController.closeViewController), for: .touchUpInside)
let barButtonItemForClose = UIBarButtonItem(customView: buttonForBack)
self.navigationItem.setLeftBarButton(barButtonItemForClose, animated: false)
}
#objc func closeViewController(){
if self == self.navigationController?.children.first {
DispatchQueue.main.async {
self.navigationController?.dismiss(animated: true, completion: nil);
}
}
else{
DispatchQueue.main.async {
self.navigationController?.popViewController(animated: true)
}
}
}

UIToolbar doesn't show again in view when dismissing keyboard in Swift

I have an UIToolbar containing a UITextField. I want to show the toolbar above the keyboard and then display it again when I finish editing inside the textfield.
The issue is that it is working when selecting the textfield. However, the toolbar is disappearing from the view when hiding the keyboard.
What should be done to redisplay it again to my view?
My Swift code is:
Add UITextFieldDelegate
class ATCChatThreadViewController: MessagesViewController, MessagesDataSource, MessageInputBarDelegate, UITextFieldDelegate {
Create ToolBar is as below
func createToolbar(){
//Fixed space
let fixed = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.fixedSpace, target: self, action: nil)
fixed.width = 10
//Camera
//let img = UIImage(named: "camera-filled-icon")!.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
let img = UIImage.localImage("camera-filled-icon", template: true)
let iconSize = CGRect(origin: CGPoint.zero, size: CGSize(width: 30, height: 30))
let iconButton = UIButton(frame: iconSize)
iconButton.setTitleColor(uiConfig.primaryColor, for: .normal)
iconButton.setBackgroundImage(img, for: .normal)
let cameraItem = UIBarButtonItem(customView: iconButton)
cameraItem.tintColor = uiConfig.primaryColor
iconButton.addTarget(self, action: #selector(cameraButtonPressed), for: .touchUpInside)
//TextField true
textFieldChat = UITextField(frame: CGRectMake(0,0,(self.view.frame.size.width - 100) ,30))
textFieldChat.tintColor = uiConfig.primaryColor
textFieldChat.textColor = uiConfig.inputTextViewTextColor
textFieldChat.backgroundColor = uiConfig.inputTextViewBgColor
textFieldChat.layer.cornerRadius = 14.0
textFieldChat.layer.borderWidth = 0.0
textFieldChat.font = UIFont.systemFont(ofSize: 16.0)
textFieldChat.delegate = self
textFieldChat.attributedPlaceholder = NSAttributedString(string: "Start typing...".localized(), attributes: [NSAttributedString.Key.foregroundColor: uiConfig.inputPlaceholderTextColor])
let paddingView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 20))
textFieldChat.leftView = paddingView
textFieldChat.leftViewMode = .always
let textFieldButton = UIBarButtonItem(customView: textFieldChat)
//Fixed space
let fixedTwo = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.fixedSpace, target: self, action: nil)
fixedTwo.width = 10
//Send Button
let imgSend = UIImage.localImage("share-icon", template: true)
let iconSendSize = CGRect(origin: CGPoint.zero, size: CGSize(width: 30, height: 30))
let sendButton = UIButton(frame: iconSendSize)
sendButton.setTitleColor(uiConfig.primaryColor, for: .normal)
sendButton.setBackgroundImage(imgSend, for: .normal)
let sendItem = UIBarButtonItem(customView: sendButton)
sendItem
.tintColor = uiConfig.primaryColor
sendButton.addTarget(self, action: #selector(sendBtnPressedWith), for: .touchUpInside)
//Flexible Space
// let flexible = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: self, action: nil)
let flexible = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.fixedSpace, target: self, action: nil)
flexible.width = 10
//Toolbar
var bottomsafeAreaHeight: CGFloat?
if #available(iOS 11.0, *) {
bottomsafeAreaHeight = UIApplication.shared.windows.first{$0.isKeyWindow }?.safeAreaInsets.bottom ?? 0
} else {
// Fallback on earlier versions
bottomsafeAreaHeight = bottomLayoutGuide.length
bottomsafeAreaHeight = UIApplication.shared.keyWindow?.rootViewController?.bottomLayoutGuide.length ?? 0
}
toolbar = UIToolbar(frame: CGRectMake(0,(self.view.frame.size.height - 116 - (bottomsafeAreaHeight ?? 0)),view.frame.width,50))
toolbar.sizeToFit()
toolbar.barTintColor = UIColor.f5f5f5ColorBg()
toolbar.isTranslucent = false
toolbar.tintColor = uiConfig.primaryColor
toolbar.items = [fixed, cameraItem, fixed, textFieldButton,fixedTwo, sendItem,flexible]
view.addSubview(toolbar)
}
// MARK: - UITextFieldDelegate
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
textFieldChat.inputAccessoryView = toolbar
return true
}
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
view.addSubview(toolbar)
self.toolbar.isHidden = false
return true
}
if only chat etxfield is using toolbar then replace line:
view.addSubview(toolbar)
with
textFieldChat.inputAccessoryView = toolbar
remove both methods textFieldShouldBeginEditing and textFieldShouldEndEditing

Font on Back Button on NavBar (Swift)

I can adjust all the other aspects of the appearance of my navigation bar - but the font of 'Back' remains stubborn.
The MWE below shows four things I have tried to no avail
1)
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name: "Helvetica-Bold", size: 4)!], for: .normal)
return true
}
2) 3) 4)
class customNavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
UIBarButtonItem.appearance().setTitleTextAttributes(
[
NSAttributedString.Key.font : UIFont(name: "Rockwell", size: 4)!,
NSAttributedString.Key.foregroundColor : UIColor.white,
], for: .normal )
navigationItem.backBarButtonItem?.setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name: "Chalkduster", size: 7)!], for: .normal)
navigationBar.topItem?.backBarButtonItem?.setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name: "AvenirNextCondensed-DemiBoldItalic", size: 4)!], for: .normal)
}
}
It's simplest in iOS 13:
let app = UINavigationBarAppearance()
app.backButtonAppearance.normal.titleTextAttributes = [
// whatever
]
UINavigationBar.appearance().standardAppearance = app
Before iOS 13, the API doesn't draw the distinction you want to draw. You just have to set the individual bar button item title text attributes for all your back buttons one at a time.
let title = // ...
let back = UIBarButtonItem(title: title, style: .plain, target: nil, action: nil)
back.setTitleTextAttributes([
// whatever
], for: .normal)
self.navigationItem.backBarButtonItem = back
(Remember also that your back bar button item is not the back bar button item when this view controller is visible, but when another view controller is pushed on top of this one.)
This solution seems to work well for me:
// Set all fonts in the navigation controller
class CustomNavigationController: UINavigationController {
// Font names
let normalFontName = "AppleSDGothicNeo-Medium"
let boldFontName = "AppleSDGothicNeo-Bold"
// Font size
let fontSize = CGFloat(13)
// Create fonts
let backButtonFont = UIFont(name: normalFontName, size: fontSize)
let titleFont = UIFont(name: boldFontName, size: fontSize)
override func viewDidLoad() {
super.viewDidLoad()
// Set standard appearance
let appearance = UINavigationBarAppearance()
appearance.backButtonAppearance.normal.titleTextAttributes = [NSAttributedString.Key.font: backButtonFont]
appearance.titleTextAttributes = [NSAttributedString.Key.font: titleFont]
UINavigationBar.appearance().standardAppearance = appearance
}
}

Navigation Bar don't charge correctly the first time

I am developing an app that contains a login and when logging in for the first time the views that go after logging do not load well the navigation bar, when opening the app for the second time and already with the credentials previously entered, this time if they load well the navigation bar.
Does anyone know why this happens?
my viewWillAppear is like this
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 38, height: 38))
imageView.contentMode = .scaleAspectFit
let image = UIImage(named: "img_header")
imageView.image = image
navigationItem.titleView = imageView
//let image_home = UIImage(named: "ic_home")
//let homeButton = UIBarButtonItem(image: image_home, style: UIBarButtonItemStyle.done, target: self, action: #selector(self.cancelMetod2))
// navigationItem.leftBarButtonItem = homeButton
let color_primary: UIColor = val_colores.hexStringToUIColor(val_colores.colorPrimary)
let color_background_primary: UIColor = val_colores.hexStringToUIColor(val_colores.ColorGrisClaro2)
//icono more
let imagen_more = UIImage (named: "ic_more_menu")!
let buttonMore: UIBarButtonItem = UIBarButtonItem(image: imagen_more, style: UIBarButtonItemStyle.done,target: self, action: #selector(self.moreMetod))
self.navigationItem.rightBarButtonItem = buttonMore
//background color
self.navigationController?.navigationBar.barTintColor = color_primary
//back button color
UIBarButtonItem.appearance().tintColor = color_background_primary
//Since iOS 7.0 UITextAttributeTextColor was replaced by NSForegroundColorAttributeName
//title color
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: color_background_primary]
//color in carrier, hour, battery
UINavigationBar.appearance().barStyle = UIBarStyle.black
self.navigationController!.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: color_background_primary]
self.navigationController!.navigationBar.barStyle = UIBarStyle.black
self.navigationController!.navigationBar.tintColor = color_background_primary
}