Swift - UISearchBar in UIToolbar - ios

How can I create A UISearchBar inside of a Toolbar in Swift? If I use the Interface Builder I get the following error:
error: Illegal Configuration: UISearchBar embedded in UIBarButtonItems (Only available in iPad documents)
Is there a Swift solution for this problem? The Toolbar will only be available on iPad.

UIToolBar is just a custom UIView, so you have three options (that I can see) for your implementation:
First, if you want to stick the search bar in the toolbar here's the code:
var searchBar = UISearchBar(frame: CGRectMake(0, 0, 100, 50))
var myView = UIView(frame: searchBar.frame)
myView.addSubview(searchBar)
var barButtonItem = [UIBarButtonItem(customView: myView)]
toolBar.setItems(barButtonItem, animated: false)
Second, if you want the search bar in the nav bar on your screen you can do the following:
let searchBar = UISearchBar(frame: CGRectMake(0, 0, 300, 50))
navigationController?.navigationBar.addSubview(searchBar)
Third, if you need the toolbar to be elsewhere, you can create your own custom UIView that looks like a toolbar (spans the width of the screen) and add the search bar to that. You can even make this new toolbar of yours a custom class so it's reusable throughout your program.
Cheers!

Related

UITextField as title view in UITableViewControllers embedded in a UIPageViewController

I have a UIPageViewController whose pages are a bunch of UITableViewController. I want to use a UITextField as the title field for each of my table view.
In my UIPageViewController's viewDidLoad, I have the following:
let titleField = UITextField(frame: CGRect(x: 0, y: 0, width: 150, height: 21))
titleField.delegate = self
titleField.placeholder = "(table name)"
navigationItem.titleView = titleField
This works and the text field shows up, but I'd like each of the page (i.e UITableViewController) to handle its own title view. So I moved the above code into my UITableViewController's viewDidLoad, but now nothing shows up.. Why?
Any input would be great. I guess I could keep track of which page I'm currently in in my UIPageViewController but I'm trying to avoid that. It's a lot of bookkeeping that I don't need.

Objective-C Tab view controller move tab bar to top

I create a tabviewcontroller in story board, I'm using Objective-C.
How can I move the position of the tab bar to the top?
I found this code in swift how can it write it in Objective-C?
okay! cool.
Are you experienced in swift?
do you know how can i write this in Objective-C?
Swift3: I achieve this by creating a custom class for UITabBarController:
class CustomTabBarController: UITabBarController {
#IBOutlet weak var financialTabBar: UITabBar!
override func viewDidLoad() {
super.viewDidLoad()
// I've added this line to viewDidLoad
UIApplication.shared.statusBarFrame.size.height
financialTabBar.frame = CGRect(x: 0, y:
financialTabBar.frame.size.height, width:
financialTabBar.frame.size.width, height:
financialTabBar.frame.size.height)
}
There's no supported way to do that with UITabBarController.
If you want a tab bar at the top of the screen, you'd have to build this yourself, or use an open source implementation.
You may find something here but there are other implementations (on GitHub etc.).
Having the tab bar at the top is an Android thing. On iOS it's always been at the bottom.
It's important to consider that all important apps (including those from Apple) have the tab bar at the bottom. Putting it at the top would mean that your app immediately feels weird for iOS users.
If you are satisfied with your swift version then below is the translated objective-c code:
- (void)viewDidLoad{
[super viewDidLoad];
self.tabBar.frame = CGRectMake( 0, [UIApplication sharedApplication].statusBarFrame.size.height, self.tabBar.frame.size.width, self.tabBar.frame.size.height);
}

Add UIView in navigation bar

I want to create UINavigationBar with rounded corner. It will look like this
What I am thinking is I add UIView with rounded corner and insert it in navigation bar. So this is my code
let roundView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 44))
roundView.backgroundColor = UIColor.whiteBackground
roundView.roundCorners(corners: [.topLeft, .topRight], radius: 20)
navigationController?.navigationBar.insertSubview(roundView, at: 0)
setTitleTextColor(color: UIColor.black)
By the UI, this works well. But then my UIBarButtonItem is missing, it covered up by my custom view and couldn't be clicked. So my question is, how to add subview in navigation bar?
Thank you!
Just not use UINavigation bar and create all by scratch. Is the easiest way. Otherwise you can try with pattern image:
navigationController?.navigationBar.backgroundColor = UIColor(patternImage: UIImage(named: "background.png"))
From Storyboard,
You have ViewController with navigationController
Select navigationController and deselect the below selected option i.e. Show Navigation Bar visibility.
Take a UIView (purpleView) with constraints
top, leading trailing = 0 w.r.t. superview
height = 64
Take another UIView (whiteView) in purpleView with constraints
top= 20 (for status bar)
leading trailing bottom= 0 w.r.t. purpleView
Now add cancel and label to your whiteview
Now your UI Hierarchy is like below
Take outlet of whiteView and make corner radius
Thats it.
If you'r not using storyboard then you can do same with code also. In this case you have to set frame of purpleView and whiteView instead of constraints.
Hope now its clear to you.
How about to make it as a normal UIView and hide the navBar and show it in the next VC, who will know which trick you have used.
read this short article here

Swift Nav Bar with Title and Page Control

I am developing a swift app but having some problem on putting title and page control, together on the nav bar. I only manage to put either one of them on the nav bar, but not both. I am hoping for something like this(as well as the animation when swiping to another view controller) :
Below is my screen output. Page controller(embedded in Navigation controller) is working, just wanted to add titles(change base on view) and page control.
And here's how I create the title:
This is how I create the title:
#IBOutlet weak var navBar: UINavigationItem!
let title: UILabel = UILabel(frame: CGRectMake(0, 0, 150, 44))
title.numberOfLines = 2
title.textAlignment = .Center
title.text = "News\n"
navBar.titleView = title
I am making the title into 2 lines to leave room for the page control. However, when I try to create page control programmatically, it appears to be "behind" the nav bar.
This is how create page control:
let pageControl : UIPageControl = UIPageControl(frame: CGRectMake(0, 0, 150, 44))
self.pageControl.numberOfPages = 3
self.pageControl.currentPage = 0
self.view.addSubview(pageControl)
Add both your label and page control to another view, setting the constraints / frames appropriately, then add that view as the titleView for the nav bar.

Add UINavigationBar programmatically to UITableViewController

I've been trying to add a UINavigationBar programmatically to my UITableViewController view. I know I can simply embed the UITableViewController to add a navigation bar, but I don't want to do that due to the way my app is setup.
I have tried the following:
var navBar: UINavigationBar = UINavigationBar(frame: CGRectMake(0, 0, 320.0, 64.0))
navBar.delegate = self
self.view.addSubview(navBar)
self.view.bringSubviewToFront(navBar)
However, the UINavigationBar just sits on the tableview (covering the first row) and scrolls with it.
The only way I have managed to make it stay (statically) at the top is to add it to self.tableView.backgroundView. Whilst this stays in position as the tableView scrolls, the tableView runs over the top of it, instead of underneath!
Can someone please shed some light on the view hierarchy here and tell me where I should be adding it?

Resources