Swift: Adding floating button to TableViewController? - ios

I'm having trouble at TableViewController.
I want to add floating button, but I found out that if I create tableview with TableviewController in Storyboard, then tableview is superview in that view controller, which means only way to add button is adding button in tableview as one of a cell, which is not floating button. (Maybe I'm wrong. I'm a bit confused. I can't add another view by Storyboard.)
I googled several times and I think the only solution is to add button by using UIWindow, but part of the solution codes are deprecated.
I hope I can get alternate solution for my problem.

Obviously the best solution is using UIViewController and adding UITableView and your button as subviews (as #Surjeet Singh suggested in comment). However if you face troubles doing this (maybe too complex right now), you can add UIButton as subview of your keyWindow as workaround. however keep in mind that you need to manually remove the button from keyWindow once your UITableViewController is going to disappear, or else your button will be appearing on other UIViewControllers. Here is the workaround solution:
func addFloatingButton() {
let keyWindow = UIApplication.shared.keyWindow
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
button.backgroundColor = .red
keyWindow?.addSubview(button)
}

Related

why Button SubView will not go to the back in swift 4?

I have a view class with xib file so I want to use this view at the background of the button so I used subview but the problem is that the subview of the button will be inFront of the button so I can't click button any more I used UI Debugging mode and I realized that the button is at the back of the view so I used send to back in my codes But still the view is inFront of the Button !
let adsView = videoAdsView()
func adsOutlet() {
self.adsView.frame = CGRect(x: 0, y: 0, width: 80, height: 80)
self.showAdsOutlet.addSubview(adsView)
self.showAdsOutlet.sendSubview(toBack: self.adsView)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
adsOutlet()
}
put following code at the end of adsOutlet method:
self.adsView.isUserInteractionEnabled = false
If you want to add your button as a subview of your background view then you can just do that, and your button will be in front of the background:
self.backgroundView.addSubview(adsButton)
If you don't want adsButton as a subview of the backgroundView but you want to make sure it's presented in front of the backgroundView then assuming that both are subview's of the parent view do this:
self.view.sendSubview(toFront:adsButton)
Please try this code:
Make the button background color clear.
Then write this code:
self.view.bringSubview(toFront: showAdsOutlet)
It may helps to you. Thank you

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?

Displaying button in subview in a stackview

I am trying to display the facebook login button in UIView but am running into some issues. I have a UIView that is currently within a stackview and I think that's causing some issues.
This screenshot shows the UIView I am trying to add the button into:
And this is where the FB button is displaying:
So I have two issues. I want the FB login button to fit the size of the UIView completely, and also be in the same location.
I have the following piece of code:
override func viewDidLoad() {
super.viewDidLoad()
let fbLoginButton = FBSDKLoginButton()
let convertToSuperView = loginStackView.convert(fbLoginButton.frame, to: self.view)
fbLoginButton.frame = CGRect(x: fbLoginView.bounds.origin.x, y: fbLoginView.bounds.origin.y, width: convertToSuperView.width, height: convertToSuperView.height)
fbLoginButton.clipsToBounds = true
fbLoginView.addSubview(fbLoginButton)
}
From reading documentation I got that the bounds is for getting the points at which something is located on the screen. I tried converting the bounds of the UIView from within the stackView to the parent view, and then giving the button the same frame as the UIView.
This clearly is not working so some assistance would be helpful. If you could explain why issues like this happen that would help me tackle issues like this in the future.
EDIT: Thanks to comments from #pierce I got it to look a little better by adding the fbLoginButton as a subview to fbLoginView.
Here I still have one issue, the frame isn't equal in size to the UIView
I also updated the code snippet above to reflect this. I have tried playing around with the sizing, but run into issues.
When I set the rect width/height to use the fbLoginView width and height, the login button is WAY too large, even with clipsToBounds its too large. When I try to center the button, it disappears.

Empty white space above UITableView inside a UIView

I'm trying to position a TableView inside my ViewController view but leaving a 44 height gap between the bottom of the navigation bar and the top of the table. I then wanted to place a UITextField inside that gap to act as a stationary header. For some reason, the TableView has an empty white space above the start of the "Prototype Cells". Its just white space. Here is what it looks like in the storyboard.
When viewing the app display, this is what it looks like:
When scrolling the table, it goes all the way up to the correct place:
Try to look in the 'attribute inspector' (in the right menu) of the Participants ViewController.
Check for the option 'Extend Edges' and uncheck the 'Under Top Bars', and then relocate your tableview.
Another possible solution is to just uncheck the option 'Adjust Scroll View Insets'.
The reason is that when the viewController extends its edges, let's say under the top bar, the tableView's scrollView automatically adjusts its inset top, so that the content of the tableView will start exactly under the top bar. But in your case this is not needed, since your tableView itself starts under the bar.
Focus on the ViewController and got to the Attribute Inspector tab:
Try this one:
self.tableView.contentInset = UIEdgeInsetsMake(-65, 0, 0, 0)
Just add this in you ViewDidLoad method
self.automaticallyAdjustsScrollViewInsets = NO;
change your table view style from grouped to plain
You should not need to change the default setting for Extend Edges.
Looks like the real problem is a blank table header view in your storyboard. It's showing in the screenshot you provided of your storyboard, right below the Enter Name view and right above the Prototype Cells view. Delete it.
My issues is, I set tableHeaderView as new UIView like this
self.tableHeaderView = UIView(frame: .zero)
Remove this line, the issue is gone. Try this:
//self.tableHeaderView = UIView(frame: .zero)
This is the 2022, iOS 15 solution if anyone stumbles upon this.
if #available(iOS 15.0, *) {
UITableView.appearance().sectionHeaderTopPadding = CGFloat(0)
}
I just found a solution for this.
In my case, i was using TabBarViewController, A just uncheck the option 'Adjust Scroll View Insets'. Issues goes away.
https://i.stack.imgur.com/qgQI8.png
For Objective-C folks, add this in your ViewDidLoad method.
[self.tableView setContentInset:UIEdgeInsetsMake(-20, 0, 0, 0)];

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