Removing the border in search bar at the navigation title - ios

my nav bar look like the image attached below. How do I remove the black border?
And my code to generate that is as follow
var searchBar : UISearchBar = UISearchBar(frame: CGRectMake(-5, 0, 320, 44))
searchBar.autoresizingMask = UIViewAutoresizing.FlexibleWidth
searchBar.layer.borderWidth = 1
searchBar.searchBarStyle = UISearchBarStyle.Minimal
var searchBarView : UIView = UIView(frame: CGRectMake(0, 0, 310, 44))
searchBarView.autoresizingMask = nil
searchBarView.layer.borderWidth = 0;
searchBarView.layer.borderColor = UIColor.clearColor().CGColor
searchBar.delegate = self;
searchBarView.addSubview(searchBar)

perhaps also do what you did on the view,
searchBar.layer.borderColor = UIColor.clearColor().CGColor

Related

How do I change the background of my UISearchbar search field in Swift 5?

How do I change the background of my searchfield? It is set to white now which makes it invisible to the user.
current look
This is my code:
//CHANGE COLOR OF NAVIGATION BAR
navigationController?.navigationBar.barTintColor = UIColor.white
navigationController?.navigationBar.isTranslucent = false
//SEARCH FIELD
let searchController = UISearchController(searchResultsController: nil)
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.delegate = self as? UISearchBarDelegate
let frame = CGRect(x: 0, y: 0, width: 300, height: 44)
let titleView = UIView(frame: frame)
searchController.searchBar.backgroundImage = UIImage()
searchController.searchBar.frame = frame
titleView.addSubview(searchController.searchBar)
navigationItem.titleView = titleView
}
It is very simple. You just need to add this line :
searchController.searchBar.backgroundColor = .gray //You can set whichever color you want here

Programmatically UISearchBar not showing up

I created a programatically UISearchBar (not UISearchController) and I set it as tableview header. However, it's not showing up. Anyone might know why is that?
Outside of the class:
var searchBar = UISearchBar()
Inside the viewDidLoad method.
searchBar.barTintColor = UIColor(red:0.54, green:0.77, blue:0.80, alpha:1.0)
searchBar.placeholder = "Cauta cheltuieli"
searchBar.backgroundImage = #imageLiteral(resourceName: "searchbarback")
tableView.tableHeaderView = searchBar
tableView.setContentOffset(CGPoint.init(x: 0, y: 44), animated: true)
You forget to give the searchbar any dimension so the size will be zero.
Try setting the dimension by setting the frame or use the constructor with a frame.
let searchbar = UISearchBar(frame: CGRect(x: 0.0, y: 0.0, height: 50.0, width: tableView.frame.width))

How to add a search bar to the navigation bar

I'd love to add a search bar managed by UISearchController to the right side of my navigation bar.
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
searchController.searchBar.placeholder = "Search"
searchController.searchBar.searchBarStyle = .Minimal
searchController.searchBar.frame = CGRect(x: 0, y: 0, width: 200.0, height: 44.0)
searchController.hidesNavigationBarDuringPresentation = false
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: searchController.searchBar)
When I tap on the search bar (so it becomes active) it covers the whole navigation bar (while overlapping everything else) instead of the little part. How can I fix its frame?
Wrapping the search bar in a UIView seems to do the trick.
searchController.searchBar.frame = CGRect(x: 0, y: 0, width: 200.0, height: 44.0)
let barContainer = UIView(frame: searchController.searchBar.frame)
barContainer.backgroundColor = UIColor.clearColor()
barContainer.addSubview(searchController.searchBar)
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: barContainer)

Custom navigation bar image and text

I need in my app a custom navigation bar with an image and a text but I can't add the text.
Here is the code to add the image, how can I add the title?
let logo = #imageLiteral(resourceName: "navigationbaricon")
let imageView = UIImageView(image:logo)
self.navigationItem.titleView = imageView
Thanks
Where is the frame assigned for self.navigationItem.titleView? Set the frame for imageView and it will work.
You can wrap the UIImageView and the UILabel (which will hold the custom title) in an UIView and then assign the UIView to the self.navigationItem.titleView. Something like this:
let view = UIView(...);
let label = UILabel(...);
label.text = "Custom Title";
let image = UIImageView(image: UIImage(named: "..."));
view.addSubview(image);
view.addSubview(label);
self.navigationItem.titleView = view;
This one is worked for me
override func viewDidLoad() {
super.viewDidLoad()
let titleView = UIView()
titleView.frame = CGRect(x: 0, y: 0, width: 100, height: 40)
titleView.backgroundColor = UIColor.redColor()
let imageView = UIImageView(image: UIImage(named: "img")!)
imageView.frame = CGRectMake(0, 0, 40, 40)
imageView.contentMode = .ScaleAspectFill
titleView.addSubview(imageView)
self.navigationItem.titleView = titleView
}

How to add multiple buttons to navigation bar without UINavigationController?

to add a multiple button to viewController inside UINavigationController,
I just this line:
self.navigationItem.rightBarButtonItems = [my button]
but i din't succeed to add that to a view wihout UINavigationController:
//create new navigation item. and add it to the nvigation bar
customNavigationItem = UINavigationItem()
/* now we need to add the menu + search to the navigation item */
//create search button
let searchButton = UIButton();
searchButton.frame = CGRectMake(0, 0, 50.0, 50.0);
searchButton.backgroundColor = UIColor.redColor();
searchButton.titleLabel!.font = UIFont.iconmoonFont(14.0)
searchButton.titleLabel?.textColor = UIColor.whiteColor()
searchButton.titleLabel!.text = IconsConstants.SEARCH;//SEARCH ICON TEXT
searchButton.backgroundColor = UIColor.clearColor();
searchButton.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Center;
searchButton.contentVerticalAlignment = UIControlContentVerticalAlignment.Center;
let searchUIbarBtn = UIBarButtonItem.init(customView: searchButton)
//create menu button
let menuButton = UIButton();
menuButton.frame = CGRectMake(0, 0, 40.0, 40.0);
menuButton.titleLabel!.font = UIFont.iconmoonFont(14.0)
menuButton.backgroundColor = UIColor.whiteColor()
menuButton.titleLabel!.text = IconsConstants.MENU;//SEARCH ICON TEXT
menuButton.backgroundColor = UIColor.clearColor();
menuButton.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Center;
menuButton.contentVerticalAlignment = UIControlContentVerticalAlignment.Center;
let menuUIbarBtn = UIBarButtonItem.init(customView: menuButton)
//add button to NavigationItem
customNavigationItem.rightBarButtonItems = [menuUIbarBtn,searchUIbarBtn];
/* add navigation bar */
navigationBar = UINavigationBar();
navigationBar.frame = CGRectMake(0, statusBarHeight, self.view.frame.size.width, 44);
//navigationBar.translucent = false
navigationBar.barTintColor = UIColor.statusBarColor()
navigationBar.items = [customNavigationItem]
self.view.addSubview(navigationBar)
i can't understand why i got a bar without any button ?

Resources