swift 4 UITableView Footer, absolute bottom - ios

I a have created a footer for my UITableView like so:
let customView = UIView(frame: CGRect(x: 0, y: self.view.bounds.height - 50, width: self.view.bounds.width, height: 50))
customView.backgroundColor = UIColor.red
commentText = UITextField(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width - 50, height: 50))
commentText.backgroundColor = UIColor.white
commentText.layer.borderColor = UIColor.gray.cgColor
commentText.layer.borderWidth = 1
commentText.borderStyle = .roundedRect
commentText.placeholder = "Enter Comment"
let submitButton = UIButton(frame: CGRect(x: self.tableView.bounds.width - 50, y: 0, width: 50, height: 50))
submitButton.setTitle("Go", for: .normal)
submitButton.backgroundColor = UIColor.blue
submitButton.addTarget(self, action: #selector(self.submitButtonPressed(_:)), for: .touchUpInside)
customView.addSubview(commentText)
customView.addSubview(submitButton)
tableView.tableFooterView = customView
and here is the result of it in the screenshot:
What I am trying to do is have the footer always at the bottom of the screen, is this possible?

A table view footer is always at the end of the table view, after the last row in the table view. This is true no matter how many rows are in the table view.
If you want a view that stays at the bottom of the screen and never scrolls no matter how many rows there are, then you don't want a table view footer. You simply want a view added to the bottom of the screen.
The best solution is to use a UIViewController that has your "footer view" pinned to the bottom and a table view added that fills the rest of the screen.

maybe you can just add the footer view at the bottom of the screen view, not add it as a footer view of UITableView, since you don't want the textfield and submit button to move when UITableView scroll.

You can just add your custom view to the view itself instead of the tableview.
view.addSubView(customView)

Related

Overlap UIButton on two or more views on swift

I am trying to overlap the button on two or more views. So when I add this on the first view it is not showing on the second view so I want one button must shown on two views.
You can make two views and embed second view to first and then button to second View and can play with its Y axis
class overlapViewcontroller:UIViewController {
private let firstView:UIView = {
let view = UIView(frame: CGRect(x: 10,
y: 100,
width: 350,
height: 50))
view.backgroundColor = .green
return view
}()
private let secondView:UIView = {
let view = UIView(frame: CGRect(x: 10,
y: 10,
width: 330,
height: 30))
view.backgroundColor = .purple
return view
}()
private let button:UIButton = {
let button = UIButton(frame: CGRect(x: 10,
y: -5,
width: 310,
height: 20))
button.setTitle("Button", for: .normal)
button.backgroundColor = .systemOrange
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemBackground
view.addSubview(firstView)
firstView.addSubview(secondView)
secondView.addSubview(button)
}
}
Did you try this? You need to add the button to the container subview and add
this line container.bringSubviewToFront(button)
https://www.hackingwithswift.com/example-code/uikit/how-to-bring-a-subview-to-the-front-of-a-uiview
You need either to
add the button as the very last so that it can be the top one on the subviews stack
or to apply the superview.bringSubviewToFront(button)

Can I align a button's label with its layout margins?

One way to left-align a UIButton's title is to set the contentHorizontalAlignment to .left (or .leading). But this places the title flush with the left edge of the button with no margin. A common way to add some margin is to set the contentEdgeInstets.
But my button extends from once edge of the screen to the other, so I would like the left and right margins to honor the layoutMargins. These margins might change as the view is resized or the device is rotated.
Is there a way to set the button's insets to observe these margins? Or should I create a button from a custom view where I can use my own label and anchor it to the layoutMarginsGuide?
I guess you can manually set the margin of the button using titleEdgeInsets to match the inset of the Cancel Button.
Have a look at the following, the two buttons are exactly the same aside from the origin.y and the titleEdgeInsets:
let view = UIView(frame: CGRect(x: 0, y: 0, width: 400, height: 400))
view.backgroundColor = .white
let button = UIButton(frame: CGRect(x: 40, y: 40, width: 200, height: 80))
button.setTitle("Some really long title", for: .normal)
button.backgroundColor = .red
button.setTitleColor(.black, for: .normal)
let button2 = UIButton(frame: CGRect(x: 40, y: 160, width: 200, height: 80))
button2.setTitle("Some really long title", for: .normal)
button2.backgroundColor = .red
button2.setTitleColor(.black, for: .normal)
button2.titleEdgeInsets = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
view.addSubview(button)
view.addSubview(button2)
It gives the following result:
Hope this helps :)
A subclassed button may work for you...
class RespectSuperviewMarginButton: UIButton {
override func layoutSubviews() {
super.layoutSubviews()
if let sv = superview {
contentEdgeInsets.left = sv.layoutMargins.left
}
}
}
My current best answer is: no, this is not possible without subclassing.
The subclass implementation I am currently using is simple enough:
class LayoutMarginRespectingButton: UIButton {
override func layoutSubviews() {
super.layoutSubviews()
contentEdgeInsets = layoutMargins
}
}

How to make a particular Table View Cell value appear in the Footer View?

Are there any tutorials, or examples to make a particular value of a table view cell from the table view return in the fixed footer view. A similar or simple concept would also be helpful. NOTE: This question is the logic part. Not for the UI design part.
Create your Custom view:
let customView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 50))
customView.backgroundColor = UIColor.red
var label = UILabel(frame: CGRectMake(0, 0, 200, 21))
label.center = CGPointMake(160, 284)
label.textAlignment = NSTextAlignment.Center
label.text = "sawthinkar"
customView.addSubview(label)
And Then add into table View footer:
tableView.tableFooterView = customView
Also you can create your custom view with xib and set the required values in the custom view.

How to set a button to most right side of navigation - iOS

I set a button to right side of navigation bar:
my view controller:
I want to set a burger icon,red cycle and a label to this button. like this:
my code:
self.navigationController?.navigationBar.barTintColor = self.utilities.hexStringToUIColor(hex: "#00b8de")
var imageview2 = UIImage(named: "menulogo")
imageview2 = imageview2?.imageResize(sizeChange: CGSize(width: 25, height: 25))
btnMenu.setImage(imageview2,for:UIControlState.normal)
btnMenu.setTitle("", for: .normal)
// setup the red circle UIView
let redCircleView = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
redCircleView.backgroundColor = UIColor.red
redCircleView.layer.cornerRadius = view.frame.size.width / 2
// setup the number UILabel
let label = UILabel(frame: CGRect(x: 30, y: 0, width: 20, height: 20))
label.textColor = UIColor.white
label.font = UIFont.systemFont(ofSize: 10)
label.text = "16"
// adding the label into the red circle
redCircleView.addSubview(label)
// adding the red circle into the menu button
btnMenu.addSubview(redCircleView)
with above codes I have three problems:
my burger image isn't in most right side of navigation.
my cycle view doesn't show
my icon is white, but it shows blue!
btnMenu is my button in navigation.
I have use this library for set badge on navigation button.
MIBadgeButton is badge button written in Swift with high
UITableView/UICollectionView performance.
https://github.com/mustafaibrahim989/MIBadgeButton-Swift

adding custom navigation bar in viewwillappear makes navigation slow?

I have created a custom navigation bar.Now i am adding this into viewwillappear method.Due to this when i return to my view controller it takes time to load.
func addViewToNav()
{
myView = UIView(frame: CGRect(x: 0, y: 20, width: (self.navigationController?.navigationBar.frame.size.width)!-40, height: (self.navigationController?.navigationBar.frame.size.height)!))
imageView.frame = CGRect(x: 5, y: (myView?.frame.size.height)!/2-14, width: 28, height: 28)
let image = UIImage(named: "download")
imageView.contentMode=UIViewContentMode.scaleAspectFill
imageView.image=image
imageView.layer.borderColor = UIColor.white.cgColor
imageView.layer.cornerRadius = imageView.frame.size.width/2
imageView.clipsToBounds = true
imageView.backgroundColor=UIColor.yellow
label = UILabel(frame: CGRect(x: imageView.frame.size.width+15, y: 0, width: 150, height: (myView?.frame.size.height)!))
label?.text="Label"
label?.textColor=UIColor.white
myView?.addSubview(imageView)
myView?.addSubview(label!)
self.navigationController?.view.addSubview(myView!)
//add tap gesturte on imageview & label
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.tapBlurButton(_:)))
myView?.isUserInteractionEnabled=true
tapGesture.numberOfTapsRequired=1
myView?.addGestureRecognizer(tapGesture)
}
Please guide how can i make it fast because this is very slow.
It is taking time because every time the controller appears on the screen viewWillAppear will be called and each time the code for creating the custom view, adding it as the subview and then adding gesture recognizer will be executed.
There are 2 ways that might reduce the processing time:
If you want to do it programatically, the add the code to create and add custom navigation bar in viewDidLoad instead of viewWillAppear
Add the custom navigation bar in the storyboard itself and create an outlet connection for it in the controller.

Resources