UIActivityIndicatorView Not Appearing After Scrolling UITableView - ios

I have a table view controller and I display a UIActivityIndicatorView in the center of the screen in viewDidLoad()
let bounds = UIScreen.mainScreen().bounds;
let width = bounds.size.width
let height = bounds.size.height
loadingIndicator = UIActivityIndicatorView(frame: CGRectMake(width / 2, height / 2, 75, 75))
loadingIndicator.layer.cornerRadius = 10
loadingIndicator.backgroundColor = kfbBlue
loadingIndicator.center = CGPointMake(width / 2, height / 2 - 37)
loadingIndicator.activityIndicatorViewStyle = .WhiteLarge
loadingIndicator.hidesWhenStopped = true
self.tableView.addSubview(loadingIndicator)
loadingIndicator.startAnimating()
This works just fine. I have a reload method for reloading the table view data. reload() works, but if I press the button that calls this method after I have scrolled the table view, the activity indicator does not appear. It only appears if the table view is scrolled up to the top.
func reload() {
loadingIndicator.startAnimating()
self.beginParsing()
}
How can I get the activity indicator to appear in the center regardless of how far the table view has been scrolled?

Don't add the UIActivityIndicatorView to the UITableView. Add it to the center pf the parent view that holds the UITableView. You are adding it to the UITableView's content view, which will scroll. Add it outside that if you want it to persist even when the UITableView has been scrolled. Most likely something like this:
self.view.addSubview(loadingIndicator)
instead of your line
self.tableView.addSubview(loadingIndicator)
If you are using a UITableViewController, the topmost view will be the TableView, so you that won't work. You will instead have to adjust the center of the activity indicator to take into account the scroll location:
loadingIndicator.center = CGPointMake(width / 2, self.tableView.contentOffset.y + height / 2 - 37)

Related

UIScrollView not scrolling with UITableView

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

UIActivityIndicatorView goes away when scrolling on UITableView

I have created an UIActivityIndicatorView in my UITableViewController in Swift like so:
indicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) as UIActivityIndicatorView
indicator.center = self.view.center
indicator.hidesWhenStopped = true
indicator.style = UIActivityIndicatorView.Style.white
indicator.backgroundColor = UIColor(white: 0.0, alpha: 0.6)
indicator.layer.cornerRadius = 15
self.view.addSubview(indicator)
self.indicator.startAnimating()
And this works like a charm, but when its running and I scroll on my UITableView Controller the UIActivityIndicatorView does not scroll with it. How do I get the UIActivityIndicatorView to scroll with the UITableViewController.
You can add it to the window
let wind = (UIApplication.shared.delegate as! AppDelegate).window!
wind.addSubview(indicator)
To remove
indicator.removeFromSuperview()
You can also make it inside the vc with
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
indicator.frame.origin = CGPoint(x: indicator.frame.origin.x, y: UIScreen.main.bounds.height / 2 - 50 + scrollView.contentOffset.y)
}
Your question and the title seem to ask different things. In the question you ask how to make it move with the scrolling, in the title you ask how not to make it scroll.
There are a few ways to do this (scrolling).
One way is to implement func scrollViewDidScroll(_ scrollView: UIScrollView) { } and move the activity indicator when the scroll view (a UITableView is a scrollView after all) scrolls. An other way (which I personally like more) is to put the activity indicator in a table view cell. Cells always scroll with the table view.
You need to do is set indicator.center to the center of its superview's bounds. Those values are in the same coordinate system (here, cell.like's coordinate system).
indicator.setCenter(CGPointMake(CGRectGetMidX(cell.like.bounds),
CGRectGetMidY(cell.like.bounds)));
I think that the indicator behind UITableView. Do you try to bring indicator to the front?
self.view.bringSubview(toFront: indicator)
Actually it scrolls without any problem, so I think there is no error in your code..

Swift 4 UIActivityIndicatorView not centered on UITableView

I am trying to center my UIActivityIndicatorView in a UITableView, this is how I am creating my UIActivityIndicatorView:
indicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) as UIActivityIndicatorView
//Set the activity indictor center
indicator.center = self.view.center
//Hide the indicator when its stopped.
indicator.hidesWhenStopped = true
//Set the style of the activity indicator
indicator.style = UIActivityIndicatorView.Style.white
//Set the background colour of the activity indicator
indicator.backgroundColor = UIColor(white: 0.0, alpha: 0.6)
//Make the activity indicator have rounded corners
indicator.layer.cornerRadius = 15
//Add activity indicator to view
self.view.addSubview(indicator)
//Start activity indicator
self.indicator.startAnimating()
But when I scroll up on my UITableView I can't see my UIActivityIndicatorView, I have tried the following:
override func viewWillLayoutSubviews() {
self.indicator.center = self.view.center
}
But that did not work.
I have also tried:
override func viewWillLayoutSubviews() {
self.indicator.translatesAutoresizingMaskIntoConstraints = true
self.indicator.frame = self.view.bounds
self.indicator.center = self.view.center
}
Also did not work, what am I doing wrong?
This is happening when I scroll down, then select an item in my table view then the activity indicator will appear.
You can't center inside the UITableViewController, as it is a scroll view, and the scroll view is the View Controller's view. However, you can center it inside the window's view like so:
// get the visible window
let window = UIApplication.shared.keyWindow!
let viewActivity = UIActivityIndicatorView(style: .whiteLarge)
// set your activity indicator's center to the center of the screen
viewActivity.center = window.center
viewActivity.hidesWhenStopped = true
// add the indicator to the active window (not your uitableviewcontroller)
// and make sure it is at the front (so it is visible)
window.addSubview(viewActivity)
window.bringSubviewToFront(viewActivity)
viewActivity.startAnimating()
You could also accomplish this by grabbing your root Navigation, or tab view controller, and centering it inside there, as well.
I had a situation where everything is fine on the UIViewController, but there were problems with the UIActivityIndicatorView on the UITableViewController.
Please pay attention to the proposed solution. In my case, activityView is stretched to the whole form and this solution helped me out.
https://stackoverflow.com/a/41886375/7938405

How to make swipe back in left side in single view

I made more than one view without taking new Controller class , I used UIgestureRecognizer but it not look better.
I want to swipe same like ios device, when we start to swipe from left side corner.
You can implement this with scrollview.
Steps:
1)First place a scrollview that fills the full screen.
2)Next create a container view which is as big as to hold your two views.
let containerSize = CGSize(width: 640.0, height: 640.0)
containerView = UIView(frame: CGRect(origin: CGPoint(x: 0, y: 0),size:containerSize))
scrollView.addSubview(containerView)
3) Now you create your view 1 , view2 and add them to container view.
4) Set the scrollview content size to container size
scrollView.contentSize = containerSize;
5) Then set the scrollview delegate method
func viewForZoomingInScrollView(scrollView: UIScrollView!) -> UIView!{
return containerView
}

Scroll view with UIWebView and labels

I have a UIWebView with labels above it (see picture), but when I scroll down, the labels just stay in place and the web view scrolls down. How can I make everything scroll down in one motion ?
self.editorialDetailWebView.loadHTMLString(editorialArticleContentThroughSegue!, baseURL: nil)
self.editorialDetailWebView.sizeToFit()
self.editorialDetailScrollView.addSubview(editorialDetailWebView)
self.editorialDetailScrollView.addSubview(editorialDetailHeadlineLabel)
self.editorialDetailScrollView.addSubview(editorialDetailAuthorLabel)
self.editorialDetailScrollView.addSubview(editorialDetailPublishDateLabel)
self.editorialDetailScrollView.addSubview(editorialDetailVolumeAndIssueLabel)
let webViewHeight = self.editorialDetailWebView.scrollView.bounds.height
let webViewWidth = self.editorialDetailWebView.scrollView.bounds.width
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
editorialDetailScrollView.contentSize = CGSize(width: self.view.frame.size.width, height: self.view.frame.size.height * 2.0)
}
Screenshot of my issue now:
Add your views to the webview's scrollview
[self.webview.scrollView addSubview:self.myLabel];

Resources