Swift Nav Bar with Title and Page Control - ios

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.

Related

Navigation Left Tab Bar Item

Hi in the app I'm building using Xcode 9.3.1 I have a navigation item titled "Home". In the next view controller, I have a sign out button in the top right corner, but I also have a "<- Home" button in the top left corner. I want to get rid of the "<- Home" button as it is unnecessary and doesn't conform to Apple's standards. Apparently, you can't delete the left tab bar item aka the (<- Home) button and I don't want to change the global tint as that would also hide the Sign out button I have in the top right corner.
Pic_of_My_Build
To hide back button:
add this in ViewDidload of your Target View controller Race groups
let maskView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 40))
maskView.backgroundColor = .clear
self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(customView: maskView)
try in your viewDidLoad from RaceGroupsViewController
self.navigationItem.hidesBackButton = true
Another option is to change the type segue from show to present modally and set a NavigationController before RaceGroupsViewController
I fixed it by putting the view controller that said "Home" in front of my navigation controller and making the view controller that said "Home" the initial view.

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.

Can't get Navigation bar customized after the first screen.

My home screen looks exactly like I want it to but the next screen just has the back button up there despite having run the same code. Here is the code I am running in the initial view controllers view did load method.
let nav = self.navigationController?.navigationBar
nav?.barStyle = UIBarStyle.Black
nav?.tintColor = UIColor.yellowColor()
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height:40))
imageView.contentMode = .ScaleAspectFit
let image = UIImage(named: "NavBarPhoto")
imageView.image = image
navigationItem.titleView = imageView
the solution is to make one root view controller where you customize your navigation bar in the viewDidLoad.
Then all your view controllers should inherit for it.
When creating the folder for the ViewController Class that was giving me trouble I accidentally just pressed enter and let Xcode name the file, well, simply file. I quickly corrected the problem, thinking that it would have no other affect. Subsequently, while running my app I left getting Unknown class PickingViewController in Interface Builder file. I suspected that all these issues were tied together and they were. I clicked on the troublesome view, added him to the current module, now on to the next problem. Thanks guys for the comments.

Swift - UISearchBar in UIToolbar

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!

Recreate google inbox's ios plus button

I'm trying to recreate google inbox's plus button on a UITableView
Here's the image:
I want to recreate the red plus button on the bottom right in a UITableView. I've got an image which is the red button and I've tried just dragging a button onto the storyboard, but it orbits around the contentView. I can't move it to the bottom right.
I also tried adding a transparent UIView as a subview of my table view like so:
let transparentView = UIView(frame: self.view.frame)
transparentView.alpha = 0
let plusButton = UIButton(frame: CGRectMake(50, 50, 50, 50))
plusButton.imageView?.image = UIImage(named: "plus")
transparentView.addSubview(plusButton)
self.view.addSubview(transparentView)
The view just doesn't show up. Not sure whats wrong. Any ideas?
You will have to position it in the superview of your table view:
o View Controller
|
\---o View
|
+---o Red Button
|
\---o Table View
Try using the side bar in Interface Builder to get your red button to the right place in the hierarchy. If you need to insert a parent view to your table view, try using Editor > Embed in > View

Resources