isTranslucent and titleTextAttributes are not working ios swift - ios

lazy var navigationBar = UINavigationBar(frame: CGRect(x: 0, y: 20, width: screen_width, height: 44))
navigationBar.barTintColor = highorange
navigationBar.isTranslucent = false
navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: grayblack]
I initialized a navigationbar, and the bartintcolor is working. But isTranslucent and titleTextAttributes are not changed.

In my sample it works.
about isTranslucent property from apple documentation
If you set this property to false on a navigation bar with a translucent custom background image, the navigation bar provides an opaque background for the image using black if the navigation bar has black style, white if the navigation bar has default, or the navigation bar’s barTintColor if a custom value is defined.
titleTextAttributes also works when i added the code below
navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.green]
let item = UINavigationItem(title: "Hello world")
navigationBar.setItems([item], animated: false)
I got following result:

Related

Transparent/translucent navigation bar with button items in Swift

I'm trying to get an almost transparent navigation bar. But I don't want the buttons on it to be transparent.
This is my code:
navigationController?.navigationBar.backgroundColor = UIColor.blackColor()
navigationController?.navigationBar.barTintColor = UIColor.blackColor()
navigationController?.navigationBar.translucent = true
navigationController?.navigationBar.alpha = 0.3
This does make it translucent but it also makes the buttons translucent/faded. How can I have a translucent bar but still have the buttons opaque?
Try this:
navigationController?.navigationBar.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.3)
navigationController?.navigationBar.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.3)
navigationController?.navigationBar.barTintColor = UIColor.blackColor()
navigationController?.navigationBar.translucent = true

Customize UINavigationBar back button

I am new to iOS development. I am creating an application with TableViewController. On that screen I added a navigation bar too. I want the title and sub title,want to customize back button. I use the following code to add the title and sub title.
func setTitle(title:String, subtitle:String) -> UIView {
let titleLabel = UILabel(frame: CGRectMake(0, 25, 0, 0))
....
let subtitleLabel = UILabel(frame: CGRectMake(0, 54, 0, 0))
....
let titleView = UIView(frame: CGRectMake(0, 0, max(titleLabel.frame.size.width, subtitleLabel.frame.size.width), 95))
.....
return titleView
}
Above code working fine. Now I want to customize the back button. My origional design should the following screenshot
. But my output not simillar it is like the following screenshot .
I am also using the following code to change the back button style
navigationController!.navigationBar.barTintColor = UIColor(red: 224/255, green: 224/255, blue: 224/255, alpha: 1.0)
navigationController?.navigationBar.backIndicatorImage = UIImage(named: "back_button")
self.navigationController!.navigationBar.backItem!.title = "";
// self.navigationController!.navigationBar.backItem!
navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "back_button")
Why my back button is different. Please someone help me to find out the issue.
set the navigation bar button item tint colour to white or default... and check
may be it will solve your problem
if you are using story board then open storyboard - Then Click the view where you have set the navigation bar image - click on that image - open attribute inspector - set the tint colour and if you have set it programatically then
self.navigationBar.barStyle = UIBarStyle.Black
self.navigationBar.tintColor = UIColor.whiteColor()

NavigationBar title doesn't appear

I add NavigationBar programmatically and later added title to it, but it doesn't appear at all. What is the problem here?
let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 55))
navigationBar.barTintColor = UIColor(red: 44/255, green: 54/255, blue: 63/255, alpha: 1)
navigationController?.navigationItem.title = "AAA"
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]
view.addSubview(navigationBar)
navigationBar appears, but title not. How to fix it?
I've tried this too:
navigationBar.topItem?.title = "BBB"
nothing again
Hope this will help you out.
// Create the navigation bar
let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 64))
// Offset by 20 pixels vertically to take the status bar into account
navigationBar.backgroundColor = UIColor.whiteColor()
// Create a navigation item with a title
let navigationItem = UINavigationItem()
navigationItem.title = "Title"
// Assign the navigation item to the navigation bar
navigationBar.items = [navigationItem]
// Make the navigation bar a subview of the current view controller
self.view.addSubview(navigationBar)
This code is working for me.
UPDATE : Swift 4/Swift 5
// Create the navigation bar
let navigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 64))
// Offset by 20 pixels vertically to take the status bar into account
navigationBar.backgroundColor = UIColor.white
// Create a navigation item with a title
let navigationItem = UINavigationItem()
navigationItem.title = "Title"
// Assign the navigation item to the navigation bar
navigationBar.items = [navigationItem]
// Make the navigation bar a subview of the current view controller
self.view.addSubview(navigationBar)
Navigation bar title is set in the view controller being displayed. Normally this is done in view did load on the view controller:
override func viewDidLoad() {
super.viewDidLoad()
self.title = "AAA"
}
EDIT: After realising that OP is adding UINavigationBar on to UIViewController and not using standard UINavigationController:
let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 55))
navigationBar.barTintColor = UIColor(red: 44/255, green: 54/255, blue: 63/255, alpha: 1)
let navigationItem = UINavigationItem.init(title: "AAA")
navigationBar.items = [navigationItem]
view.addSubview(navigationBar)

Changing the Navigation Bar in Swift

I would simply like to know how to make my navigation bar look like the one on the left in the image below:
My app uses a navigation controller, and the navigation bar by default looks like the screen on the right in the image. I don't like it being so small and would like it higher and also to be able to change the colour and the font of the text used for the header.
I would appreciate any help in how to do this and where to place the code.
thanks
There are many ways to costumize your navigation bar:
//You can change the bar style
navigationController?.navigationBar.barStyle = UIBarStyle.Black
// You can change the background color
navigationController?.navigationBar.barTintColor = UIColor(red: 4 / 255, green: 47 / 255, blue: 66 / 255, alpha: 1)
// You can add a logo on it
let navBarImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
navBarImageView.contentMode = .ScaleAspectFit
let navBarImage = UIImage(named: "myNavBarLogo.png")
navBarImageView.image = navBarImage
navigationItem.titleView = navBarImageView
//You can change the text color
navigationController?.navigationBar.tintColor = UIColor.yellowColor()
//You can change the font
if let font = UIFont(name: "AppleSDGothicNeo-Thin", size: 34) {
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: font]
}
You just need to play with those attributs to get the Navigation Bar as you want.
I hope that helps you!
You can change the navigation bar in Interface Builder.
You can change the background color by changing the bar tint. You can also change the title font, color, and etc., as you can see below:

Make navigation bar transparent regarding below image in iOS 8.1

I try to set my navigation bar transparent regarding a image below this, something like the following image :
I tried the solution in transparent navigation bar ios but I don't get the above result, I get only the icon on the left but without any color in the navigation bar, completely transparent. But if I set a background color the transparency disappears at all.
There is any way to set a color in the navigation bar and make it transparent??
Thanks in advance.
just checked on the 8.1 simulator and got very similar result to your picture
let bar:UINavigationBar! = self.navigationController?.navigationBar
bar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
bar.shadowImage = UIImage()
bar.backgroundColor = UIColor(red: 0.0, green: 0.3, blue: 0.5, alpha: 0.3)
main point here is background color with alpha.
Check attached image, maybe I missed something?
To set this style globally, use the UIAppearance APIs. In AppDelegate's application:didFinishLaunchingWithOptions: add the following code:
// Sets background to a blank/empty image
UINavigationBar.appearance().setBackgroundImage(UIImage(), forBarMetrics: .Default)
// Sets shadow (line below the bar) to a blank image
UINavigationBar.appearance().shadowImage = UIImage()
// Sets the translucent background color
UINavigationBar.appearance().backgroundColor = UIColor(red: 0.0, green: 0.3, blue: 0.5, alpha: 0.3)
// Set translucent. (Default value is already true, so this can be removed if desired.)
UINavigationBar.appearance().translucent = true
Have you tried setting the navigationBar's alpha property? In your root view controller to the navigation controller...
[self.navigationController.navigationBar setBackgroundColor:[UIColor greenColor]];
[self.navigationController.navigationBar setAlpha:0.3f];
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.backgroundColor = UIColor.clear
self.navigationController?.navigationBar.isTranslucent = true
If the above code is not working then set edgesForExtendedLayout to all.
self.edgesForExtendedLayout = .all

Resources