Happens only when compiled with Xcode9 and iOS 11 devices.
Section header does not automatically stick to the top leaving a space in the view through which content can be seen. (Screenshot included)
Happens when tapped for full screen.
But some times the header sticks to the top on the tap action
On scrolling up/down the header sticks to the top. (GIF included)
Screenshot
GIF
Correct me if I am wrong, the floating nature of section header is provided by UITableViewStyle set to plain.
tableView = UITableView()
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 44
tableView.cellLayoutMarginsFollowReadableWidth = false
tableView = UITableView(frame: self.view.frame, style: UITableViewStyle.plain)
tableView.sectionHeaderHeight = 44
// tableView.estimatedSectionHeaderHeight = 0
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "content")
tableView.register(UITableViewHeaderFooterView.self, forHeaderFooterViewReuseIdentifier: "hContent")
tableView.dataSource = self
tableView.delegate = self
Tried to set the estimatedSectionHeaderHeight to 0 (Disable it) as suggested here. But it did not work.
We tried reloading the tableView data in our hideNavBar func as suggested here for the similar problem. But that would scroll us to some other content.
Tried the following as suggested by other answer in our hideNavBar function and again facing the same issue where we are scrolled to some distant content.
tableView.reloadData()
tableView.layoutIfNeeded()
tableView.beginUpdates()
tableView.endUpdates()
The change could be due to change in safe area constraints with iPhone X. So, instead of reloading the whole data, I suggest adding a reference for the frame.
A workaround that worked perfectly for me even in iPhone X.
Add the following lines in the respective functions.
func hideNavbar() and hideTabbar ()
tableView.frame = CGRect(x: 0, y: 0.01, width: view.bounds.width, height: view.bounds.height)
func showNavbar() and hideTabbar ()
tableView.frame = CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height)
Related
I am trying to implement a scrollView that contains a text field, button, and table view. I am completely lost trying to get the thing to scroll, I've tried changing the height of UITableView but that has not worked, I've tried adjusting the height of the content View and scrollView, but that has not worked either. How can I get the scrollview to scroll and adjust the size when another tableViewCell is added to the tableView?
override func viewDidLayoutSubviews() {
tableView.frame = CGRect(x: tableView.frame.origin.x, y: tableView.frame.origin.y, width: tableView.frame.size.width, height: CGFloat(tableView.numberOfRows(inSection: 0) * 120))
tableView.reloadData()
}
#IBAction func addExercisePressed(_ sender: Any) {
test.append("nice")
tableView.reloadData()
tableView.frame = CGRect(x: tableView.frame.origin.x,y: tableView.frame.origin.y ,width: self.view.frame.size.width, height: CGFloat(test.count * 120))
scrollView.frame.size = CGSize(width: self.view.frame.size.width, height: tableView.frame.size.height)
}
Please dont use tableView inside a scrollView, tableView is subclass of scrollView and that's mean tableView already have scroll function
your answer to set
tableHeight.constant = tableView.contentSize.height
It will make recycle/dequeue cell function useless because tableView will show all the cell immediately, tableview will call cellForRowAt for all indexes that showed in tableview viewport
UITableview inside Scrollview call cellforrow too many times
Here why we use dequeueReusableCellWithIdentifier
From what I see you want something like this right
you can check this repo how I only use a tableview to achieved that
please note that tableHeaderView is scrollable, sectionHeaderView is sticky
All I needed was to set a height constraint for the TableView and connect that via IBOutlet, then this line of code wherever reloading table data is needed
tableHeight.constant = tableView.contentSize.height
I am using a grouped UITableView in XIB. When loaded its showing extra space on top of first section header.
What I have tried so far:
if #available(iOS 11, *) {
self.recordTableView.contentInsetAdjustmentBehavior = .never
} else {
//Doesn't Work
//self.automaticallyAdjustsScrollViewInsets = false
self.parent?.automaticallyAdjustsScrollViewInsets = false
}
Also tried setting tableView -> Content Insets to Never in XIB.
What am I missing here?
Well you will need to understand two things. Setting zero height of TableHeaderView and SectionHeaderHight
This will set your Table Header Height to zero
tableView.tableHeaderView = UIView(frame: CGRect(origin: .zero, size: CGSize(width: self.view.frame.size.width, height: CGFloat.leastNormalMagnitude)))
This will set section header hight (NOTE : YOU might not need this. Depends on your use case)
tableView.sectionHeaderHeight = CGFloat.leastNormalMagnitude
Programmatically i have created a View and added a label and buttons to it. its fine in vertical it aligns to centre but as i rotate the screen it does not aligns to centre rather it seems as left aligned.
This is my code:
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 150))
headerView.layer.borderWidth = 1
headerView.layer.borderColor = UIColor.whiteColor().CGColor
headerView.backgroundColor = ClientConfiguration.primaryUIColor()
let myLabel = UILabel()
myLabel.frame = CGRectMake(-1, -1, tableView.frame.width, 30)
myLabel.font = UIFont.boldSystemFontOfSize(14)
myLabel.backgroundColor = ClientConfiguration.primaryUIColor()
myLabel.textColor = UIColor.whiteColor()
myLabel.text = "Select Time Zone"
myLabel.textAlignment = .Center
let frame = CGRectMake(1, 1,headerView.frame.width , 70)
self.btnTimeZone.frame = frame
headerView.addSubview(myLabel)
headerView.addSubview(self.btnTimeZone)
self.buttonTitileString = self.selectedZone.value
self.btnTimeZone.setTitle(buttonTitileString, forState: .Normal)
return headerView
}
In horizontal mode the button text and label are aligned to left
and when i set self.btnTimeZone.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Center
its fine in horizontal mode but in vertical mode they all are right aligned as;
enter image description here
How can i solve this issue i need both of them in centre aligned in both horizontal and vertical mode.
You just need to reload the table when rotate the phone. Implement following method and inside of this method reload the tableView
override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
tableView.reloadData()
}
In case of the application should support both portrait and landscape modes, the good approach would be to work with NSLayoutConstraint.
Of course you have both options to create them programmatically or from the Interface Builder, I'd like to note that you are able to create the header view as a cell, without the need of doing it programmatically; That's -obviously- will leads to the ease of setup the desired constrains for the header subviews.
Try this also
change
tableView.frame.width
to
tableView.frame.size.width
You could try to set autoresizing for parent view.
headerView.autoresizesSubviews = true
myLabel.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.btnTimeZone.autoresizingMask = [.flexibleWidth, .flexibleHeight]
In Swift 5 (and 3, 4 too) you got to override viewDidLayoutSubviews() method of life cycle of view controller, it calls after viewWillTransition() method when rotating your device.
In viewDidLayoutSubviews() method you just need to reload the data of your tableView in order to redraw your section header for portrait to landscape or landscape to portrait, and it works perfectly.
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.tableView.reloadData()
}
Thanks to GayashanK for give me this idea with his answer.
So I am using a UITableView to display information about different films.
At the top of the VC, I have a UIImage which sits inside of a UIView. And then my table sits underneath. The table currently sits right up against the bottom of the image (which is what I want), see below:
The Issue
I followed a tutorial to add a simple effect, so when the user pulls down on the tableView, the image enlarges. You can see what I mean by seeing the tutorial here: See here
This all worked wonderful and gave my the effect I wanted, however, it's now added an empty space below the image, see the image below:
Everything still works fine, and the effect works as expected, but this space is now there - which I really don't want.
The settings in the storyboard for this VC are set as followed:
The code I added to make the effect is as follows:
private let KTableHeaderHeight: CGFloat = 160.0 // which is the height of my UIImage
var headerView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
headerView = tableView.tableHeaderView
tableView.tableHeaderView = nil
tableView.addSubview(headerView)
tableView.contentInset = UIEdgeInsets(top: KTableHeaderHeight, left: 0, bottom: 0, right: 0)
tableView.contentOffset = CGPoint(x: 0, y: -KTableHeaderHeight)
updateHeaderView()
}
And then:
func updateHeaderView() {
var headerRect = CGRect(x: 0, y: -KTableHeaderHeight, width: tableView.bounds.width, height: KTableHeaderHeight)
if tableView.contentOffset.y < -KTableHeaderHeight {
headerRect.origin.y = tableView.contentOffset.y
headerRect.size.height = -tableView.contentOffset.y
}
headerView.frame = headerRect
}
override func scrollViewDidScroll(scrollView: UIScrollView) {
updateHeaderView()
}
If I comment out all the code I've added, it then looks fine again, so I'm guessing it is this code that's causing the space.
I'm really keen to understand why this gap has formed, and how I can remove it, still using the code added to make the image enlarging effect.
Update
My UIImageView layout Attributes:
Your issue is likely the private let KTableHeaderHeight: CGFloat = 160.0 line, which doesn't equal the height of the imageView in the header.
You need to find out the fixed height of the imageView after scaling, which you can get by multiplying the original image height by view width/original image width, and set the imageView height and private var KTableHeaderHeight: CGFloat to that value.
If your using a grouped UITableView then it makes a space for the group, not sure how to get rid of it. You might want to consider making it a plan UITableView and then create the sections headers that you want via the delegate
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
// create UIView here
}
However, after playing with the sample project, it seems like you can adjust your kTableHeightHeader variable in the top to adjust to hide the top section header.
I modified the sample project so you can see it here
Swift code:
tableView.tableFooterView = UIView()
Objective C code:
tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
I have decided to use UIScrollView instead of UITableView in my app for many reasons. When I was using UITableView, I was able to add a UIRefreshControl with no issue whatsoever. When I use the same code on my UIScrollView however, nothing happens. I have tried several third-party refresher libraries, and none seem to work. Any help is appreciated.
Source:
var scroll: UIScrollView!
var refreshControl: UIRefreshControl!
override func viewDidLoad()
{
super.viewDidLoad()
self.scroll = UIScrollView(frame: self.view.frame)
self.scroll.contentSize = CGSize(width: self.view.bounds.width, height: 10)
self.refreshControl = UIRefreshControl()
self.refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
//self.refreshControl.addTarget(self, action: "loadPostsIntoCards", forControlEvents: UIControlEvents.ValueChanged)
self.scroll.addSubview(self.refreshControl)
self.view.addSubview(self.scroll)
//self.loadPostsIntoCards()
}
The loadPostsIntoCards method does my API call, and creates UIViews ("cards") that are added to the UIScrollView. I then change the contentSize of the UIScrollView based upon the total height of all the cards. The cards are added perfectly, and the scroll view acts as expected, except with regards to the UIRefreshControl.
Thanks in advance for any help!
EDIT:
After some fooling around I found another solution that will allow the scrollview to scroll (and trigger the refresh control) even when the contentSize is smaller than the scrollView height. Just set the alwaysBounceVertical property to true:
self.scroll.alwaysBounceVertical = true
OLD ANSWER:
Your content size should be larger than your scrollview's height in order to make this work. Change this line:
self.scroll.contentSize = CGSize(width: self.view.bounds.width, height: 10)
To:
self.scroll.contentSize = CGSize(width: self.view.bounds.width, height: self.view.bounds.height+1)
That would allow your RefreshControl to kick in.
Or, in your case you should probably calculate the height of your cards and then set that as the height for your contentSize.