Recreate google inbox's ios plus button - ios

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

Related

How to show a custom view on top of navigation bar in Swift?

So the way I understand navigation bar (navigation item) is that it has three locations you can modify, which is left (leftBarButtomItem), middle (titleView), and right (rightBarButtonItem).
Now what I'm going to achieve is that I want to just add a simple progress bar line at the very bottom of navigation bar, but still inside navigation bar. I want to make this like an extension of navigation bar that I can reuse on other screens. But I want that left, middle, and right "views" are still working like usual. e.g. I don't want that if I change the title view content manually in other view controller, then the line disappears / stops working for that other view controller. So this will feel like an independent overlay added on top of navigation bar as subview, separated from leftBarButtonItem, titleView, and rightBarButtonView, sort to speak.
Is it possible to do that in navigation item?
This is a example of how to add a Image instead of a Title String
fileprivate func setupTitle(){
let logo = UIImage(named: "my_incredible_logo")
let imageView = UIImageView(image:logo)
imageView.contentMode = .scaleAspectFit
self.navigationItem.titleView = imageView
self.navigationController?.navigationBar.prefersLargeTitles = false
}

Static/fixed Navigation Bar in iOS

I have a Navigation Controller and a Collection View under it inside my app. And there is a problem: I use large title inside my Navigation bar, so everything inside is not static. When I scroll the collection view cells, the title (I created it manually using UILabel() to move it as I want inside the navigation bar) and buttons move up and the navigation bar takes form of iOS 10 navigation bar, I mean its height. You can see it here:
The normal state of my Navigation Bar with "Prefer large titles" On:
It happens when I scroll my Collection View, everything goes up:
So the question is simple: how to make the force constant height for the navigation bar? I want it to become fixed even while scrolling. Are there any ideas? Is it possible?
And the second question, if the first is impossible: Another solution for my problem is to make the Navigation Bar with "Prefer large titles" Off bigger. I tried this code:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let height: CGFloat = 50 //whatever height you want to add to the existing height
let bounds = self.navigationController!.navigationBar.bounds
self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: bounds.width, height: bounds.height + height)
}
but it worked only for large titles. So how can I make the navigation bar bigger?
Yes, you can make it fixed. It will not scroll if the very first view in the view hierarchy is not a CollectionView/TableView (ScrollView).
Using Storyboard/Xib:
Consider the following image where a tableView and button are added in scene. Here the navigation bar will collapse on scroll of tableView because tableView is the very first view in viewController's containerView hierarchy attached to the navigation bar.
Now to make the navigation bar fixed, if we just change the order of tableView and button as below, it will disable the collapsing of navigation bar.
To change the order of the view, you have to click, hold and move up/down.
If you have only CollectionView in this scene then you can add a placeholder view at the top and set its height to zero as below,
Programmatically:
If you are setting up view's programmatically then you just need to add a placeholder view at the top or add tableView/collection after adding other views.
e.g,
self.view.addSubview(UIView(frame: .zero))
self.view.addSubview(tableView) // or collectionView

Display a UIView / UIControl overlapping UIToolbar

I'm trying to display a large button at the bottom of the screen, so that it appears above the toolbar.
Button Overlapping Toolbar
My first attempt at this works on the iPad, and on the iPhone in Landscape mode, but the button appears behind the toolbar in Portrait mode. So, this is probably related to the difference in rendering with the Split View Controller:
addButton.frame = CGRect(x:0, y:0, width:48, height:48)
addButton.setBackgroundImage(UIImage(named:"Button - Add"), for: .normal)
let topView = self.navigationController?.view
topView?.addSubview(addButton)
I don't want it to appear above all other view controllers, e.g. popovers and modal segues, so I can't place the button on the application's topmost window, i.e. the following doesn't give me the right result either:
let topView = UIApplication.shared.windows.first
topView?.addSubview(addButton)
The only solution that works for me is to add the button to the toolbar, but this isn't great because the touch zone for the button is clipped by the toolbar:
self.navigationController?.toolbar.addSubview(addButton)
let topView: UIView? = addButton.superview
So, does anyone know of a way to place a UIView or UIControl in a layer or view above the toolbar, but still on the active viewController?

How to properly create an overlay view in storyboard?

My aim is to create a popup view right below a button once is clicked, this is the concept:
The way I prepared the storyboard is by using two different UIViewController. The main one contains the two buttons and, once clicked, I modally push the second UIViewController over the current context.
My question is: what's the best or properly way to create constrain in order to move the "Tapped" image right below the button?
The way I do this is to add a childcontroller to your first storyboard vc. Then adjust the frame of the container view in the storyboard, which I hope will be easier than programmatically adjusting frames.
Then be sure to change the class on top of the VC that Xcode created for you.
When the button is clicked get the the bottom position of it (it's y in frame + height) and send it to the popup , then set them as the top constraint of the imageView in popup in viewDidLayoutSubview
You can calculate the position of the overlay using the button's and the view's positions and dimensions. For example, the overlay's y can be the button's frame.maxY + constant.
let x = ...
let y = ...
let width = ...
let height = ...
let overlay = UIView(frame: CGRect(x: x, y: y, width: width, height: height))
view.addSubView(overlay)
The translucent black view can be added as a UIView with the same frame as the view of the VC and constrained to always be the same as the frame of the VC's view. Add this view first, before you add overlay.

How to bring a subview's subview to top?

The following print screen shows my layout:
I have the main view , then a Top view and then a picture.
When I press on the picture, I want a semi transaprent black screen appearing on the entire view:
let scrennRect = UIScreen.mainScreen().bounds
let coverView = UIView(frame: scrennRect)
coverView.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.3)
self.view.addSubview(coverView)
But I want my picture to stay on top of the coverView ?
Is there a way I can bring only the picture to be in front of the coverView ?
The problem is that your coverView and imageView have different super views.
That's why you cannot bring the imageView to the front.
In order to bring the imageView to the front you need to move it outside of the Top View so your View contains the Top view and the imageView.
Then, you can add the coverView to the main view, and bring the imageView to the front.
Another solution:
Place an image view inside the coverView at the exact same position.
This should do your work. Any view maintains a stack of its subviews.
self.view.addSubView(blackTransparentView);
self.view.bringSubViewToFront(myImageView);

Resources