UiView on top of the TableView - ios

I'm trying to put a UIView (banner) on top of the list (tableView), so the UIview will not disappear when the user scrolls down the list.
I tried this code, but didn't work.
override func viewDidLayoutSubviews() {
self.view.addSubview(banner)
banner.frame.size.width = self.view.frame.size.width
}
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
var rect = self.banner.frame
rect.origin.y = max(0,scrollView.contentOffset.y + scrollView.contentInset.top)
self.banner.frame = rect
}
Any advice how to fix it? Thank you

Instead of trying to keep the view within the scrollView, a much easier method would be to use a standard UIViewController with a UIView attached to the top using AutoLayout with constraints and UITableView attached to the bottom of the UIView and the bottom of the UIViewController.
With this layout, only your smaller tableView would be scrolling and your UIView would be stationary in place, outside the scope of the scrolling UITableViewCells.
If you are currently using an UITableViewController, you'll need to remove the override modifiers from your tableView methods, make your UIViewController implement UITableViewDataSource and UITableViewDelegate, then attach the delegates either in code or storyboard.
Your new UIViewController would look like this at the top:
class MyController: UIViewController, UITableViewDataSource, UITableViewDelegate {
...
}

Related

How to increase and decrease UIView height by codebase using Swift

In my case, I have to add UIView in top of the Tableview but it should be inside Tableview because of scrolling. Here, I drag and dropped UIView on topside tableview. Now, some scenario I need to increase and reduce height of the UIView. I tried to add constraints but its not available. How to achieve this by codebase?
Tableview with UIView Storyboard Hierarchy
Storyboard Design
Disabled Constraints For UIView
Constraints is not working with table header view. you can set table header using custom view and update its frame
Set a custom view
Set table header and update it's frame
class ViewController: UIViewController {
#IBOutlet var tableView: UITableView!
#IBOutlet var tableHeader: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
loadHeader()
}
func loadHeader() {
tableHeader.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 300)
tableView.tableHeaderView = tableHeader
}
}

How to access a scroll view sub class from the main view controller (Beginner)

Using swift 3, I have a UIScrollView in my main view. I call it my "feature scroll view"
I have also added a custom class to my UIScrollView, which has one function in it called loadFeatures()
featureScrollView.swift:
class featureScrollView: UIScrollView {
public func loadFeatures () {
//Add Pictures to the UIScrollView
}
How do i call loadFeatures() on the UIScrollView instance that exists from my ViewController.swift class in my main view?
In my ViewController.swift I want to simply write
scrollView.loadFeatures()
and have the code in loadFeatures() apply to the scrollView that is currently on the screen. I am beginning learning Swift and I appreciate everybody's help greatly!
image of ScrollView and corresponding class
Bind the IBOutlet of scrollview (Scrollview that you have shown in https://i.stack.imgur.com/5HRH0.png) in your ViewController.swift and just call loadFeatures() from ViewController.swift with that outlet instace.
ex., once you bind outlet like as below
class ViewController: UIViewController {
#IBOutlet weak var scrollView: featureScrollView!
override func viewDidLoad() {
super.viewDidLoad()
scrollView.loadFeatures()
}
}
Since you've already made a custom class for your ScrollView you could easily access that by subclassing your scrollView in your *.storyboard
For example:
You could set the custom class of your scrollView by placing it in here
Which would look like this
Note: This is just one way of doing things
Another is to do it manually inside your UIViewController class like this
class ViewController: UIViewController {
var ftScrollView: FeatureScrollView!
override func viewDidLoad() {
super.viewDidLoad()
ftScrollView = FeatureScrollView() // I Initialized the scrollView
ftScrollView.frame = // some frame here
view.addSubView(ftScrollView)
ftScrollView.loadFeatures()
}
}
PS: Please conform to swift coding standards. You should always start class names with a capital letter, thus this class featureScrollView: UIScrollView should be named like this class FeatureScrollView: UIScrollView

How to add padding to left/right of a static cell in UITableView swift

I'm using a UITableViewController with static cells and I want to make it so that the cells do not take up the entirety of the view. I want something more akin to this image: https://i.stack.imgur.com/4nO09.png
I can't quite figure out how to do so. I've been able to change the height thanks to self.tableView.contentInset, but I'm not sure how to change the width.
How would I do this?
EDIT:
Here's my Code for Fay007 as well as an image.
import UIKit
class ContactFormViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Sets the background images
if let patternImage = UIImage(named: "Pattern") {
view.backgroundColor = UIColor(patternImage: patternImage)
}
}
override func viewWillAppear(_ animated: Bool) {
let numberOfRows: CGFloat = CGFloat(self.tableView.numberOfRows(inSection: 0))
let headerHeight: CGFloat = (self.view.frame.size.height - (self.tableView.rowHeight * numberOfRows)) / numberOfRows
self.tableView.contentInset = UIEdgeInsetsMake(headerHeight, 0, -headerHeight, 0)
}
http://imgur.com/a/Q2kfH
The first image I linked has its cells away from the left/right edges, as in my comment I explained I believe they did using autolayout. Since the tableview is a subview of the UIView of the UIViewController, I believe one would be able to assure that. however, when using a UITableViewController, which is required to use static cells in a UITableView, there is no UIView parent.
One easy way of doing this is as follows: In storyboard, or interface builder, add a UIView subview to the UITableviewcell. Create constraints that define your desired distance of this subview to the edges of the cell.
To add rounded corners, you can do so within awakeFromNib by setting the cornerRadius of the subview's layer property to your desired radius.

UIView from CGRectMake does not have the right height

I am adding a UIView to a container view programmatically, (the container view however is created in storyboard). Here is the code:
class ViewController: UIViewController{
#IBOutlet weak var dwView: UIView!
private var dwSelector = dwSelectorView()
override func viewDidLoad(){
super.viewDidLoad()
addDWSelector()
}
func addDWSelector(){
dwSelector.setTranslatesAutoresizingMaskIntoConstraints(false)
dwSelector.frame = CGRectMake(self.dwView.bounds.origin.x, self.dwView.bounds.origin.y, self.dwView.bounds.width / 2.0, self.dwView.frame.height)
println("dw height: \(self.dwView.frame.height)")
//prints 568, way too large of a value
self.dwView.addSubview(dwSelector)
}
}
The heigh of dwView is 123 in storyboard but the print state printed 568 and so now this is what it looks like:
You should always not rely on -(void)viewDidLoad since view bounds is incorrect at this point or - (void)viewWillAppear if you are using auto layout to set your view's frame. If you layout view in UIViewController, viewDidLayoutSubviews() is a appropriate place, if you layout subviews in UIView, it is layoutSubviews().
Check this article to get more details:Where to progmatically lay out views in iOS 5 (and handling orientation changes)
have you tried to call addDWSelector() in viewWillAppear()?

UITableView is not showing inside a UIScrollView

I have in my View a long UIScrollView (about 1000 px in height), and at the end of this UIScrollView I have a UITableView.
The cellForRowAtIndexPath is never called (surely i checked the delegate and datasource if they are connected right, and the IBOutlet of the table is strong) but numberOfRowsInSection is getting called.
I tried reloading the TableView when the UIScrollView scrolls so when the table is at focus the cellForRowAtIndexPath might get called, but with no luck.
Did anyone encounter a similar behaviour when trying to use tableview and scrollview together?
Your hierarchy is like this:
A parentView is there. Inside the parent view there is a scroll view and there is a table view. So, your tableview is somewhere at 1000 from origin of parentview.
So, tableview will never become visible to your parentview and no delegates will be fired.
Include your view as a headerView of your UITableView like that:
Update for 2020, swift 5.X. To create a custom UITableView that allows your tableview inside a scrollview!
1) Create a subclass of UITableView:
import UIKit
final class ContentSizedTableView: UITableView {
override var contentSize:CGSize {
didSet {
invalidateIntrinsicContentSize()
}
}
override var intrinsicContentSize: CGSize {
layoutIfNeeded()
return CGSize(width: UIView.noIntrinsicMetric, height: contentSize.height)
}
}
2) Add a UITableView to your layout and set constraints on all sides. Set the class of it to ContentSizedTableView.
3) You should see some errors, because Storyboard doesn't take our subclass' intrinsicContentSize into account. At runtime it will use the override in our ContentSizedTableView class

Resources