Lable and button in the header section view UITableView - ios

I am trying to add a section title Label and a button in the header section view. but it looks empty. when I run the application the header is empty. the 2nd section code work fine
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if (section == 0){
let label = UILabel.init(frame: CGRect.init(x: 17, y: 139, width: tableView.frame.size.width, height: 45))
label.textColor = UIColor.black
label.font = UIFont.systemFont(ofSize: 13.0)
label.textAlignment = .left
label.text = " My Balances"
label.backgroundColor = UIColor(red: 0.95, green: 0.95, blue: 0.95, alpha: 1.00)
let frame = tableView.frame
let height:CGFloat = 66
let button = UIButton(frame: CGRect(x: 306, y: 139, width: 15, height: 15)) // create button
button.tag = section
// the button is image - set image
button.setImage(UIImage(named: "remove_button"), for: .normal)
let headerView = UIView(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: height)) // create custom view
headerView.addSubview(button) // add the button to the view
headerView.addSubview(label)
return headerView
//return label
//return label
}
else {
let label = UILabel.init(frame: CGRect.init(x: 0, y: 241, width: tableView.frame.size.width, height: 45))
label.textColor = UIColor.black
label.font = UIFont.systemFont(ofSize: 13.0)
label.textAlignment = .left
label.text = " My Customers"
label.backgroundColor = UIColor(red: 0.95, green: 0.95, blue: 0.95, alpha: 1.00)
return label
}
}

You are not following the proper way. First you have to set height of header view using heightForHeaderInSection from tableview object in viewDidLoad() like -
tableView.heightForHeaderInSection = 250
or by using its delegate method -
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 250
}
You set the height of header view equal to tableview height. Set it less than it let height:CGFloat = 250 -
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let label = UILabel.init(frame: CGRect.init(x: 0, y: 241, width: tableView.frame.size.width, height: 45))
label.textColor = UIColor.black
label.font = UIFont.systemFont(ofSize: 13.0)
label.textAlignment = .left
label.text = " My Balances"
label.backgroundColor = UIColor(red: 0.95, green: 0.95, blue: 0.95, alpha: 1.00)
let frame = tableView.frame
let height:CGFloat = 250
let button = UIButton(frame: CGRect(x: 5, y: 10, width: 15, height: 15)) // create button
button.tag = section
// the button is image - set image
button.setImage(UIImage(named: "remove_button"), for: .normal)
let headerView = UIView(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: height)) // create custom view
headerView.addSubview(button) // add the button to the view
headerView.addSubview(label)
return headerView
//return label
}
Or another way is to make custom reusable header view, register as header view and finally dequeu it.
You can follow the documentation from apple for second way - https://developer.apple.com/documentation/uikit/views_and_controls/table_views/adding_headers_and_footers_to_table_sections

Related

cannot add UIButton to tableView section

I have this code
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 45))
headerView.backgroundColor = UIColor.white
let label = UILabel(frame: CGRect(x: 0, y: 12.5, width: tableView.frame.width - 100, height: 20))
label.numberOfLines = 0
label.font = UIFont(name: "SFProDisplay-Bold", size: 18);
label.textColor = UIColor.darkGray
let sectionName: String
switch section {
case 0:
sectionName = NSLocalizedString(" Featured", comment: "featured")
case 1:
sectionName = NSLocalizedString(" Trending", comment: "trending")
// ...
default:
sectionName = NSLocalizedString(" New", comment: "new")
}
label.text = sectionName
label.adjustsFontForContentSizeCategory = true
headerView.addSubview(label)
let button:UIButton = UIButton(
frame: CGRect(x: tableView.bounds.size.width-100,
y: 12.5,
width: 100,
height: 20))
button.setTitle("More", for: .normal)
button.titleLabel?.textColor = UIColor.black
button.backgroundColor = UIColor.white
// button.addTarget(self, action: #selector(btnShowHideTapped), for: .touchUpInside)
button.titleLabel?.textColor = UIColor.black
headerView.addSubview(button)
return headerView
}
I am trying to add the UIButton to the tableView header, but it is not working. Does anyone know why?
The UIButton does not show up why the label does.
This line does not work:
button.titleLabel?.textColor = UIColor.black
Replace it with the following line:
button.setTitleColor(.black, for: .normal)

add drop shadow on a table view header

I have this following header for my table view header section
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView()
view.backgroundColor = .white
// add a button
let button = UIButton(type : .system)
button.showsTouchWhenHighlighted = false
button.frame = CGRect(x: 0 , y: 0 , width: 20 , height: 20 )
if(collapsed[section])
{ button.backgroundColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)}
else
{ button.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)}
button.addTarget(self, action: #selector(reloadTable), for: .touchUpInside)
button.tag = section
view.addSubview(button)
button.bindFrameToSuperviewBounds()
// add an Image
let image = UIImageView(image: UIImage(named: "sup1"))
image.frame = CGRect(x: 150 , y: 5 , width: 100 , height: 100)
image.contentMode = .scaleAspectFit
view.addSubview(image)
// add a label
let lblNew = UILabel()
lblNew.frame = CGRect(x: 20, y: 100 , width: 100, height: 30)
lblNew.text = "١٢٣ ر.س"
lblNew.textColor = UIColor.myDarkGreen
lblNew.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(lblNew)
// view.dropShadow()
return view
}
Now I want to add drop shadow effect on the section header to look like this
I have tried some solutions here but it works on simple view that is not used as a viewForHeaderInSection, any tips?
Use the view's layer's shadow properties:
view.layer.shadowOpacity = 1
view.layer.shadowOffset = CGSize(width: 0, height: 1)
view.layer.shadowRadius = 1
Result:

How to manage textlabel height by parent view

I have some issue. I trie to make custom table header sections. Here is my code:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let sectionHeader = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 36))
sectionHeader.backgroundColor = #colorLiteral(red: 0.1215686277, green: 0.01176470611, blue: 0.4235294163, alpha: 1)
tableView.backgroundColor = colors.background
let headerName = UILabel(frame: CGRect(x: 15, y: 0, width: tableView.frame.size.width, height: sectionHeader.frame.size.height))
headerName.font = UIFont(name: "Helvetica", size: 12)
headerName.text = sectionHeaders[section]
headerName.backgroundColor = #colorLiteral(red: 0.9529411793, green: 0.6862745285, blue: 0.1333333403, alpha: 1)
headerName.clipsToBounds = true
sectionHeader.addSubview(headerName)
return sectionHeader
}
And here is result:
With blue and yellow color i marked and noticed, that label height not the same as headerview. Can some one help me how manage textlabel height to header view height, and how to align text verticaly
label height not the same as headerview
At first you should setup height of your section header:
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 36.0
}
Based on height: 36 from let sectionHeader = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 36))
how to align text verticaly
headerName.textAlignment = .center

How to programatically fix a view to a tab bar in Swift?

I have a tableView inside a navigationController inside a tabBarController.
I want to programatically fix a view above the tabbar like this:
.
I have experimented with adding constraints without success, also because I don't know whether to use self.view or self.bottomLayoutGuide of self.tableView or self.navigationController or self.tabBarController.tabBar or ... any suggestions?
Try this:
button = UIButton(frame: CGRect(x: 0, y: self.view.frame.height - 99, width: self.view.frame.width, height: 50))
button.backgroundColor = UIColor(red: 0, green: 88/255, blue: 171/255, alpha: 1)
button.tintColor = .white
button.titleLabel?.font = UIFont(name: "Helvetica", size: 20)
button.setTitle("YOUR TEXT", for: .normal)
button.addTarget(self, action: #selector(yourAction), for: .touchUpInside)
self.navigationController?.view.addSubview(button)
In my example, it is a UIButton but it can be a UIView too
Same example with UIView
let view = UIView(frame: CGRect(x: 0, y: self.view.frame.height - 99, width: self.view.frame.width, height: 50))
view.backgroundColor = UIColor(red: 0, green: 88/255, blue: 171/255, alpha: 1)
self.navigationController?.view.addSubview(view)
self.automaticallyAdjustsScrollViewInsets = NO;
then you will see the tableviewcell covered by navigationbar and
if you want navigationbar to be full transparency
self.navigationController.navigationBar.backgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault;
self.navigationController.navigationBar.shadowImage:[UIImage new];
Try this Swift 3.0
Bottom view code here.
let bottomview = UIView(frame: CGRect(x: 0, y: self.view.frame.height-63,width:self.view.frame.width, height: 80))
bottomview.backgroundColor = UIColor.red
self.view.addSubview(bottomview)
Table view code here...
let table: UITableViewController = MyTableViewController()
let tableView: UITableView = UITableView()
tableView.frame = CGRect(x: 0, y:self.navigationController.navigationBar.frame.size.height, width: self.view.frame.width, height: 500)
tableView.dataSource = table
tableView.delegate = table
self.view.addSubview(tableView)

Unable to modify UILabel frame inside UICollectionView?

I have ViewController and inside it UICollectionView, I'm trying to add UILable to UICollectionView programmatically :
let Label: UILabel = {
let label = UILabel()
label.text = "Sample Text Here"
label.textAlignment = .center
label.textColor = UIColor(white: 0.4, alpha: 0.4)
label.font = UIFont.systemFont(ofSize: 20)
return label
}()
override func viewDidLoad() {
super.viewDidLoad()
Label.translatesAutoresizingMaskIntoConstraints = false
Label.frame = CGRect(x: 0, y: 40, width: view.frame.width, height: 20)
collectionView.addSubview(Label)
}
when I tried to change the y value it's the same
when I tried to add it like this : view.addSubView(Label) didn't appear at all.
note: the label should appear if there's no data to populate the cells
It works..
override func viewDidLoad() {
super.viewDidLoad()
let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: 300, height: 300), collectionViewLayout: UICollectionViewFlowLayout())
self.view.addSubview(collectionView)
collectionView.backgroundColor = UIColor.red
let label = UILabel()
label.text = "Sample Text Here"
label.textAlignment = .center
label.textColor = UIColor(white: 0.4, alpha: 0.4)
label.font = UIFont.systemFont(ofSize: 20)
label.frame = CGRect(x: 0, y: 100, width: view.frame.width, height: 20)
collectionView.addSubview(label)
}
Be careful with adding labels directly to the collectionview, though.
You could create a uiview and add both label to it and collection (with the appropriate collection view layout of course). Use collection view only for displaying cells and add anything to the cell.contentView if you wish
Thanks.

Resources