Is it possible to change large navigation bar height?
self.navigationItem.largeTitleDisplayMode = .always
self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationController?.navigationBar.addSubview(expandedNavBar)
I dont think you can change navigationBar height because of Apple HIG. If you want to change navigationBar height , you can remove navigationBar and you can show your own customview to user like navigationBar
If you want to make the navigationBar a little bigger, you can try this trick by adding an empty string to the prompt property.
self.navigationItem.prompt = ""
Ok, I found this way:
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.minimumLineHeight = 200
let attributes: [NSAttributedString.Key: Any] = [ .paragraphStyle: paragraphStyle]
let navigationBar = navigationController?.navigationBar
if #available(iOS 13, *) {
if let appearance = navigationBar?.standardAppearance {
appearance.largeTitleTextAttributes = attributes
navigationBar?.standardAppearance = appearance
navigationBar?.scrollEdgeAppearance = appearance
}
} else {
navigationBar?.largeTitleTextAttributes = attributes
}
Related
I created a simple demo and only created a UITabBarController's subclass and set in storyboard.
I want to set the TabBarButtonItem's title to an orange color when selected and black color when normal. The following code works fine on any iOS version on iPhone, but on iOS 15's iPad (both device and simulator) the selected color changes to blue and wired normal state color.
Is this an Apple bug or have I missed something?(I'm using Xcode13)
class CustomViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let tabBarAppearnace = UITabBarAppearance()
let tabFont = UIFont.boldSystemFont(ofSize: 18)
let selectedAttributes: [NSAttributedString.Key: Any]
= [NSAttributedString.Key.font: tabFont, NSAttributedString.Key.foregroundColor: UIColor.orange]
let normalAttributes: [NSAttributedString.Key: Any]
= [NSAttributedString.Key.font: tabFont, NSAttributedString.Key.foregroundColor: UIColor.black]
tabBarAppearnace.stackedLayoutAppearance.normal.titleTextAttributes = normalAttributes
tabBarAppearnace.stackedLayoutAppearance.selected.titleTextAttributes = selectedAttributes
tabBar.standardAppearance = tabBarAppearnace
}
}
For iPadOS you have to use the inlineLayoutAppearance attribute, because on iPad the items in the TabBar are displayed inline by default (title and icon are displayed next to each other).
But in fact you should also configure compactInlineLayoutAppearance because otherwise your custom styling won't apply if you are using landscape mode on an iPhone for example.
class CustomViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let tabBarAppearnace = UITabBarAppearance()
let tabFont = UIFont.boldSystemFont(ofSize: 18)
let selectedAttributes: [NSAttributedString.Key: Any]
= [NSAttributedString.Key.font: tabFont, NSAttributedString.Key.foregroundColor: UIColor.orange]
let normalAttributes: [NSAttributedString.Key: Any]
= [NSAttributedString.Key.font: tabFont, NSAttributedString.Key.foregroundColor: UIColor.black]
tabBarAppearnace.stackedLayoutAppearance.normal.titleTextAttributes = normalAttributes
tabBarAppearnace.stackedLayoutAppearance.selected.titleTextAttributes = selectedAttributes
//New
tabBarAppearnace.inlineLayoutAppearance.normal.titleTextAttributes = normalAttributes
tabBarAppearnace.inlineLayoutAppearance.selected.titleTextAttributes = selectedAttributes
tabBarAppearnace.compactInlineLayoutAppearance.normal.titleTextAttributes = normalAttributes
tabBarAppearnace.compactInlineLayoutAppearance.selected.titleTextAttributes = selectedAttributes
tabBar.standardAppearance = tabBarAppearnace
}
}
For more info: https://developer.apple.com/documentation/uikit/uitabbarappearance
If anyone is interested, you can also achieve this in storyboard for iOS 15, Xcode 13:
In storyboard, highlight the Tab Bar
Open Inspector (top right button on the Xcode window)
Open the Attribute Inspector (three slidy bars)
Under Tab Bar, check the Appearance box: Standard and/or Scroll Edge
Then scroll down and find the Standard Layout Appearance
Set the stacked option to Custom
Now you should see the Standard Stacked Layout Appearance properties
For State Config set to Normal, make sure to set the Title and Icon Colors.
Then also change the State to Selected and set the Title and Icon colors.
Now we need to configure the Inline Layout for iPad
So now we need to do the same thing for the Inline Layout, in the same section in the Attribute Inspector, you will the Inline property, change that option to Custom. and set the just like the steps above.
I would recommend you also do the same for the Compact Inline Layout.
If you are using storyboard instead of coding this, then you may want to consider configuring the Scroll Edge appearance as well, you would have to repeat duplicate everything we just did for Standard Appearance for the Scroll Edge appearance.
I am trying to add a background image to my nav bar using the UINavigationBarAppearance. However, the imageView size is bigger than the navigation bar content view.. How do I fix this?
Result: Notice the imageview overlaps with the safearea
Heiarchy debugger
Code:
private func setupNavBar() {
navigationItem.largeTitleDisplayMode = .never
guard let navigationController = navigationController else { return }
let appearance = navigationController.navigationBar.standardAppearance.copy()
appearance.configureWithTransparentBackground()
appearance.backgroundImage = UIImage.checkmark
appearance.backgroundImageContentMode = .scaleAspectFit
appearance.backgroundColor = .red
navigationController.navigationBar.standardAppearance = appearance
}
Found an alternative. Instead of using the appearance background image, I set the titleView on the navigation item.
let imageView = UIImageView(image: UIImage.checkmark)
imageView.contentMode = .scaleAspectFit
navigationItem.titleView = imageView
How do I set a transparent background for the sections index bar in a UITableView?
I've tried:
tableView.sectionIndexBackgroundColor = .clearColor()
but the bar gets a light grey background
EDIT
I've tried to change background color of the tableview but it doesn't seem to influence.
tableView.backgroundView = UIView.init()
tableView.backgroundColor = .blackColor() // tried .clearColor() too.
EDIT2
tableView.sectionIndexBackgroundColor = UIColor(white: CGFloat(1.0), alpha: CGFloat(0.5)) // change the alpha and check once
I've also tried a color with alpha values. I can make the background semi-transparent (see line code above). But if I set alpha to 0.0 to make it completely transparent, I get again the light grey background.
tableView.sectionIndexBackgroundColor = .clear works perfect
For swift 4.2 you must set viewBackground for your section and for that view set background color.
class CommonSection: UITableViewHeaderFooterView { static let
lightSectionColor = UIColor.gray.withAlphaComponent(0.005)
func setup(title: String, sectionColor: UIColor? = nil) {
textLabel?.text = title
textLabel?.font = UIFont.preferredFont(forTextStyle: .largeTitle)
guard let color = sectionColor else { return }
self.backgroundView = Init(value: UIView()) {
$0.backgroundColor = color
} } }
Note: Init just shortcut for initializing elements.
I have a UITableView to which I add programmatically a UISearchController as a header. Since I'm adding it programmatically, I can't change the font from the storyboard. I need to change the UISearchBar font to match the font of the rest of the app. I would need to either change the font of a UISearchBar or of the whole tableViewHeader
I saw that there's a similar question with an answer in objective-c, however I'm developing my app in Swift and I tried to convert the answer on the other question to the following code but it still won't work :
let lightFont = UIFont(name: "GothamRounded-Light", size: 15)
for subview in searchController.searchBar.subviews {
if subview is UITextField{
(subview as! UITextField).font = lightFont
}
}
From this question
You'll need to loop through it's subviews to find the UITextField (not accessible by property)
for subview in search.subviews {
if subview is UITextField {
(subview as! UITextField).font = UIFont(name: "Arial", size: 24.0)
}
}
Then you can set it's font.
Try this:
let textFieldInSearchBar = searchBar.value(forKey: "searchField") as? UITextField
textFieldInSearchBar?.font = UIFont.systemFont(ofSize: 10)
Add below code in viewDidLoad()
let textFieldInsideUISearchBar = searchBar.value(forKey: "searchField") as? UITextField
let placeholderLabel = textFieldInsideUISearchBar?.value(forKey: "placeholderLabel") as? UILabel
placeholderLabel?.font = UIFont(name: "Avenir Next", size: 13.0) //this will set custom font to placeholder font
textFieldInsideUISearchBar?.font = placeholderLabel?.font //this will set custom font to text font
This will set a custom font to both the placeholder and text in the UISearchBar.
if #available(iOS 9.0, *) {
UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).textColor = UIColor.blueColor()
UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).font = UIFont.systemFontOfSize(17)
}
I'm trying to create a search bar like this:
But I'm noticing that I'm probably going to have to replace the search bar with my own image because the search bar corners comes out wrong when I set:
self.searchController.searchBar.layer.cornerRadius = 50 // I've tried other numbers besides this too with no luck
self.searchController.searchBar.clipsToBounds = true
If I set this:
self.searchController.searchBar.layer.cornerRadius = self.searchController.searchBar.bounds.height/2
The search bar comes out like this:
Which still isn't exact like in the image.
Is there a way to replace the left and right side of the textfield with an image that way I can use the rounded corners from my custom search bar?
I am using this code UISearchBar but you can use this code with UISearchController.
let searchBar = UISearchBar()
searchBar.sizeToFit()
searchBar.placeholder = "Search"
navigationItem.titleView = searchBar
if let textfield = searchBar.value(forKey: "searchField") as? UITextField {
textfield.textColor = UIColor.blue
if let backgroundview = textfield.subviews.first {
// Background color
backgroundview.backgroundColor = UIColor.white
// Rounded corner
backgroundview.layer.cornerRadius = 14;
backgroundview.clipsToBounds = true;
}
}
You should change the radius of searchTextField inside UISearchBar .
you can do that like this :
searchBar.searchTextField.layer.cornerRadius = 20
searchBar.searchTextField.layer.masksToBounds = true
* searchBar is an outlet of UISearchBar from storyBoard
The issue here is you are setting the corner radius on the UISearchBar, not the UITextField inside it. You can do some sort of hack to get the UITextField, but that's not really recommended.
As you mentioned in your question, you'll need to use custom images and the methods shown here: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UISearchBar_Class/#//apple_ref/doc/uid/TP40007529-CH3-SW40
This IS working for me in swift 3 iOS 10:
searchController.searchBar.layer.cornerRadius = 20
searchController.searchBar.clipsToBounds = true
ez way for searchbarview
for subview & POPUPs [Swift 5]
override func layoutSublayers(of layer: CALayer) {
searchBarPopup.clipsToBounds = true
searchBarPopup.layer.cornerRadius = 10
searchBarPopup.layer.maskedCorners = [ .layerMaxXMinYCorner, .layerMinXMinYCorner]
}