Change colour of back bar button item only in swift - swift

I am trying to change the colour of the back bar button item in swift navigation bar.
Ultimately I aim to make something line this for the navbar:
This is my current code which gives me three back arrows as the back button item but how do I change the colour so it is three different colours in one bit of text? (Green, Yellow, Red).
func setCustomBackImage() {
navigationItem.backBarButtonItem = UIBarButtonItem(title: "<<<", style: .plain, target: nil, action: nil)
}

There is a much more easier way, since you said " I'm still fairly new to swift ", here you go !
//add this in AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions
launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let backArrowImage = UIImage(named: "icon50")
let renderedImage = backArrowImage?.withRenderingMode(.alwaysOriginal)
UINavigationBar.appearance().backIndicatorImage = renderedImage
UINavigationBar.appearance().backIndicatorTransitionMaskImage = renderedImage
return true
// This will change all back-arrows to whatever image you want
}
Here "icon50" should that "<<<" image. Td:lr , use an image.
Hope it helps :)

This seems to be something like what you're describing:
Pretty simple if you use an image instead of a title for your bar button item.

Related

Globally Change The Border Color Of UITextField?

I'm trying to globally change the UITextField color to white when my application loads.
I call my Theme manager like:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let theme = ThemeManager.currentTheme()
ThemeManager.applyTheme(theme: theme)
return true
}
Then in my theme manager, I set the border color like:
UITextField.appearance().layer.borderWidth = 5.0
UITextField.appearance().layer.borderColor = UIColor.white.cgColor
UITextField.appearance().textColor = UIColor.white
It seems like every other UI elment takes the changes fine, but this doesn't persist throughout the application. If I manually set the color on specific TextFields it works fine, but was trying to avoid that, am I doing something wrong?
Thanks.

NavigationBar title overlapped with new opened VC after active and cancel search bar in 1st VC

I have a navigation bar which includes a search bar to filter the table data in current VC, let's call it 1st VC.
And the steps is active this search bar and cancel it. Then click any row of tableView 1st VC to open 2nd VC. The weird behavior is coming, the navigation title of 2nd VC is overlapped with 1st VC's nav title. Also the button on top-right is not clickable anymore after this issue happened. I get this when upgrade to iOS 13 from previous version 12.
/ / / attach the issue screenshot firstly, you could see that title "Music" in 1st VC is overlapped with title "Playing" in 2nd VC.
/ / / nav bar code in 1st VC
override func viewDidLoad() {
super.viewDidLoad()
// set tableView's separatorColor
self.tableView.separatorColor = UIColor(rgb: 0x5E5E5E)
// get songs from app's local dir
self.retrieveSongsfromLocalDir()
// add search bar
resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.hidesNavigationBarDuringPresentation = false
controller.obscuresBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
// set search Bar covered color, same with tableView's background color.
controller.searchBar.barTintColor = UIColor(rgb: 0x292f33)
return controller
})() // closure, learn it later!!
navigationItem.searchController = resultSearchController
// reload the tableView
self.tableView.reloadData()
}
/ / / I put the custom code of navigation bar in AppDelegate.swift for global use.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Custom Navigation Bar's appearance.
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().barTintColor = UIColor(rgb: 0x55acee)
UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().clipsToBounds = false
// UINavigationBar.appearance().backgroundColor
UINavigationBar.appearance().titleTextAttributes = [.foregroundColor: UIColor.white, .font: UIFont.boldSystemFont(ofSize: 23)]
return true
}
/ / / Update, it is duplicate with thread https://stackoverflow.com/questions/58134631/ios-13-uibarbuttonitem-not-clickable-and-overlapping-uinavigationbars-when-using. It seems the bug of Apple from iOS 13, and temporary fix is to set hidesNavigationBarDuringPresentation = true.

swift - implement the func to change background color for the whole screen for multiple tabs

my app has 04 tabs, i want to write a func to change the background color for the whole screen: status bar, naviation bar, etc.
i want this because when i change color in the func then the whole app will change
below i just change to the top. but too complicated
what i have done is change the status bar text to white
//change text of status bar to white
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
go to the Storyboard, Select the View Controller and in the Editor menu Select Embed in Navigation Controller. Select the Navigation Bar and in the Attribute Inspector set the Bar Tint color to red.
in appDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
UINavigationBar.appearance().barStyle = .blackOpaque
return true
}

Correct and incorrect answer in Swift

I'm making simple quiz app about countries in Europe, I have a map and three buttons below with names of countries, one of them is correct and of course two incorrect. I want to highlight incorrect (if user click incorrect button) button for red and correct for green, if user click correct button I want to highlight it for green and after maybe 5s come back to the same color that was at first. I know how to change button color but I don't know ho do that for 5s and come back to default color. How can I do that ? Below its code that I use to change button color
UIButtonOutlet.Backgroundcolor.Uicolor.green
But its default green, so I can't set my color.
You can try something like this. First declare one UIButton instance in your class and then scheduledTimer after setting backgroundColor of button for correct's and incorrect and store that button reference with button that you have created first.
var selectedButton = UIButton()
Now use this selectedButton when you are setting button's backgroundcolor.
btnOutlet.backgroundcolor = .green //For correct
btnOutlet.backgroundcolor = .red //For incorrect
self.selectedButton = btnOutlet
//Now scheduled the timer for 5.0 sec
Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(setButtonBGBack), userInfo: nil, repeats: false)
Add the setButtonBGBack method in your class
func setButtonBGBack() {
self.selectedButton.backgroundcolor = .blue //Set to default here
self.selectedButton = UIButton()
}
Lets say you have your code for changing the color which is in the "changeColor" func:
var correctButton: UIButton
func changeColor() {
if correctButton.backgroundColor == .green {
correctButton.backgroundColor = .lightGray //back to original color
}
}
What this essentially does is changes your correct button color back to its default (whatever that may be) if it is green when the func is called.
Now to use this, we can do a little work inside of the IBAction that is connected to each of your buttons:
#IBAction func buttonClicked(sender: UIButton) {
if sender.titleLabel?.text == "Correct Answer" {
button.backgroundColor = .green
correctButton = button //set the correct button variable so the changeColor func can be used
let timer = Timer.init(timeInterval: 5, target: self, selector: #selector(changeColor), userInfo: nil, repeats: false)
} else if sender.titleLabel?.text == "False Answer 1" || sender.titleLabel?.text == "False Answer 2" {
button.backgroundColor = .red
}
}
So in this code, if you click the button that is the correct answer (you can identify this by tag or other means, but I just decided to use its text) then the color is set to green immediately, and then the correctButton variable is set, but then a timer is initiated that after five seconds will call your changeColor func and then change it back to its original color.
Hope this helps :)
Edit:
Of course, my method assumes that you are using the storyboard to create these buttons. If you are creating them programmatically, then NiravD's method will work better.

After button click label goes up in left upper corner

I have set auto-layout for the label so it should be centered right above my button, however when I click on the button the label goes up in the left corner. I'm a totally new beginner to coding, so bare in mind I might have just missed something very simple? I would guess in my code I need some kind of way to tell the position of the label to be after the click?
This is my code for the button action:
nextStagetwo.textAlignment = NSTextAlignment.Center
nextStagetwo.userInteractionEnabled = true
nextStagetwo.text = "Go to next stage"
self.view.addSubview(nextStagetwo)
let gestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTap:")
nextStagetwo.addGestureRecognizer(gestureRecognizer)
}
}
func handleTap(gestureRecognizer: UIGestureRecognizer) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc: AnyObject! = storyboard.instantiateViewControllerWithIdentifier("stagethree")
self.presentViewController(vc as! UIViewController, animated: true, completion: nil)
}
Run your application in Xcode 7 and select the debugger view and then the debugger toolbar button with tooltip, "Debug View Hierarchy" (third button from right in the debugger toolbar). That will show you what auto layout is computing for each item on the screen. You can peel off layers as needed in this view. Compare those values with your auto layout constraints for that item and its containers.
Also, make sure you don't have any auto layout warnings in the Xcode Interface Builder for your storyboard.
I added this to the code, which was what was missing in my code:
let nextStagetwo = UILabel(frame: CGRectMake(250, 250, 250, 250))
nextStagetwo.center = CGPointMake(210, 200)