MXSegmentedPager overlaps with TableView - ios

Segmented Pager Adapter is overlapping with my TableView when creating it manually
Using following library
https://github.com/maxep/MXSegmentedPager
Following is my code
let tabVC = AppStrings.appStoryBoard.instantiateViewController(withIdentifier:
"aboutReviewViewControllerID") as! AboutReviewViewController
viewControllerArray.append(tabVC)
segmentedPager = MXSegmentedPager(frame: CGRect(x: 0, y: 0, width:
self.view.frame.width, height: view.frame.height))
view.addSubview(segmentedPager)
segmentedPager.dataSource = self
segmentedPager.delegate = self
segmentedPager.backgroundColor = .clear
segmentedPager.parallaxHeader.view = topBGView
segmentedPager.parallaxHeader.mode = .fill
segmentedPager.parallaxHeader.height = topBGView.frame.height
segmentedPager.parallaxHeader.minimumHeight = 10
segmentedPager.parallaxHeader.delegate = self
segmentedPager.reloadData()
[![enter image description here][1]][1]func segmentedPager(_ segmentedPager: MXSegmentedPager, viewForPageAt index: Int) -> UIView {
return viewControllerArray[index].view
}
func segmentedPager(_ segmentedPager: MXSegmentedPager, segueIdentifierForPageAt index: Int) -> String {
return ""
}

Related

How to change page control dot size and spacing in swift?

I want customize page control like a image.
I've already search that, but there are only deal scale.
I want change width, height, spacing.
How can I do that?
I tried this
class DefaultPageControl: UIPageControl {
override var currentPage: Int {
didSet {
updateDots()
}
}
func updateDots() {
let currentDot = subviews[currentPage]
subviews.forEach {
$0.frame.size = ($0 == currentDot) ? CGSize(width: 16, height: 4) : CGSize(width: 8, height: 4)
$0.layer.cornerRadius = 2
}
}
}
But how to change distance??
#oddK Can you try with this below answer. It's my assumption.
class DefaultPageControl: UIPageControl {
override var currentPage: Int {
didSet {
updateDots()
}
}
func updateDots() {
let currentDot = subviews[currentPage]
let spacing = 5.0
subviews.forEach {
$0.frame = ($0 == currentDot) ? CGRect(x: 0, y: 0, width: 16, height: 4) : CGRect(x: spacing, y: 0, width: 8, height: 4)
//$0.frame.size = ($0 == currentDot) ? CGSize(width: 16, height: 4) : CGSize(width: 8, height: 4)
$0.layer.cornerRadius = 2
}
}
}
The default UIPageControll is not flexible.
class ExtendedpageControll: UIView{
var numberOfPage: Int
var currentpage : Int = 0{didSet{reloadView()}}
var currentIndicatorColor: UIColor = .black
var indicatorColor: UIColor = UIColor(white: 0.9, alpha: 1)
var circleIndicator: Bool = false
private var dotView = [UIView]()
private let spacing: CGFloat = 6
private lazy var extraWidth: CGFloat = circleIndicator ? 6 : 4
init(numberOfPages: Int,currentPage: Int,isCircular: Bool){
self.numberOfPage = numberOfPages
self.currentpage = currentPage
self.circleIndicator = isCircular
super.init(frame: .zero)
configView()
}
required init?(coder: NSCoder) {fatalError("not implemented")}
private func configView(){
backgroundColor = .clear
(0..<numberOfPage).forEach { _ in
let view = UIView()
addSubview(view)
dotView.append(view)
}
}
private func reloadView(){
dotView.forEach{$0.backgroundColor = indicatorColor}
dotView[currentpage].backgroundColor = currentIndicatorColor
UIView.animate(withDuration: 0.2) {
self.dotView[self.currentpage].frame.origin.x = self.dotView[self.currentpage].frame.origin.x - self.extraWidth
self.dotView[self.currentpage].frame.size.width = self.dotView[self.currentpage].frame.size.width + (self.extraWidth * 2)
}
}
override func layoutSubviews() {
super.layoutSubviews()
for (i,view) in dotView.enumerated(){
view.clipsToBounds = true
view.layer.cornerRadius = bounds.height / 2
let width: CGFloat = circleIndicator ? self.bounds.height : CGFloat(self.bounds.width / CGFloat(self.numberOfPage) - self.spacing) - self.extraWidth
UIView.animate(withDuration: 0.2) {
view.frame = CGRect(x: ((self.bounds.width / CGFloat(self.numberOfPage)) * CGFloat(i)) + self.spacing, y: 0, width: width , height: self.bounds.height)
}
}
reloadView()
}
}
Usage: If you want to link ExtendedpageControll to a View Such as CollectionView Just Do like this: (item is your Datamodel)
class SampleViewController: UIViewController{
let colectionView = UICollectionView()
lazy var pageControll: ExtendedpageControll = {
let pc = ExtendedpageControll(numberOfPages: items.count, currentPage: 0,isCircular: true)
pc.currentIndicatorColor = .black
return pc
}()
func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
if pageControll.currentpage == indexPath.row {
guard let visible = self.collectionView.visibleCells.first else { return }
guard let index = self.collectionView.indexPath(for: visible)?.row else { return }
pageControll.currentpage = index
}
}
}
inside init, you can set the shape of the indicator to be circular or extended via isCircular.

Reuse Identifier on iCarousel

I am trying to build a carousel on my app based on this project, https://github.com/nicklockwood/iCarousel, only I am writing it in swift. I have it working correctly, where you can scroll through and tap on the item at index correctly here:
func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {
let tempView = UIView(frame: CGRect(x: 0, y: 130, width: 295, height: 300))
let object = self.imageFilesArray.object(at: index) as! PFObject
let imageView = UIImageView()
imageView.frame = CGRect(x: 0, y: 0 , width: 295, height: 295)
imageView.layer.cornerRadius = 15
imageView.layer.masksToBounds = true
let imageFile = object.object(forKey: "Image") as! PFFile
imageFile.getDataInBackground{
(data, error) -> Void in
if error == nil {
}
imageView.image = UIImage(data: data!)
tempView.addSubview(imageView)
}
return tempView
}
func carousel(_ carousel: iCarousel, didSelectItemAt index: Int) {
let cell = carouselView.itemView(at: index)
let object = self.imageFilesArray[index] as! PFObject
if cell?.tag == 0 {
print("open cell")
let moveImg = CGAffineTransform(translationX: 0, y: -100)
UIView.animate(withDuration: 0.4, delay: 0.1, options: [], animations: {
//cell.imageView.transform = moveImg
}, completion: nil)
cell?.tag = 1
} else if cell?.tag == 1 {
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "EventDetailViewController") as! EventDetailViewController
self.present(nextViewController, animated:false, completion:nil)
}
However, what I am trying to do is when I tap the item at index, the imageView transitions on the screen. My issue is that I cannot access that particular cells imageView in order to move it. Does anyone know what I need to change?
You could give your imageView a tag like this:
let imageView = UIImageView()
imageView.frame = CGRect(x: 0, y: 0 , width: 295, height: 295)
imageView.layer.cornerRadius = 15
imageView.layer.masksToBounds = true
imageView.tag = 2
Then you can retrieve the imageView from the cell like this:
if let imageView = cell.viewWithTag(2) as? UIImageView {
imageView.transform = moveImg
}
OR
You could filter through the subviews of the cell and find the first subview that can be cast to a UIImageView. Although this isn't very robust, consider creating a Class and XIB for this cell.
cell.subviews.flatMap { $0 as? UIImageView }.first?.transform = moveImg

Memory Usage with UICollectionView extremely high

I am having an issue with memory usage. Whenever I scroll or reload the data the memory usage keeps going up and never goes down. I have a feeling that for some reason the cells are not releasing any of the items displayed.
Any help would be greatly appreciated. I am quite sure I am doing something wrong, but I can't seem to find what.
Thank you in advance
Here is the code for creating my UICollectionView:
import Foundation
import UIKit
import CoreData
import Crashlytics
public class podCollectionView : UIViewController,UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout,UIPopoverPresentationControllerDelegate {
private var collectionView : UICollectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout()) // Initialization
private var inmates : NSArray = [];
private var context : NSManagedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext;
private var configuration = Configuration.sharedInstance;
private var titleView = UILabel();
private var database : FMDatabase = (UIApplication.sharedApplication().delegate as! AppDelegate).database!;
public override func viewDidLoad() {
getInmates();
super.viewDidLoad();
self.collectionView = podMainView(x: "1", y: "2");
self.collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "CellCell") // UICollectionViewCell
self.collectionView.backgroundColor = UIColor.clearColor();
self.collectionView.layer.cornerRadius = 5;
self.collectionView.layer.masksToBounds = true;
self.collectionView.delegate = self // delegate : UICollectionViewDelegate
self.collectionView.dataSource = self // datasource : UICollectionViewDataSource
self.view.addSubview(self.collectionView);
self.view = self.collectionView;
self.navigationItem.setHidesBackButton(true, animated:false);
var inmateRefresh: NSTimer!
gameTimer = NSTimer.scheduledTimerWithTimeInterval(120, target: self, selector: #selector(refreshDAta), userInfo: nil, repeats: true);
}
public func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.inmates.count;
}
public func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let retVal : UICollectionViewCell = self.collectionView.dequeueReusableCellWithReuseIdentifier("CellCell", forIndexPath: indexPath);
retVal.prepareForReuse();
retVal.addSubview(InmateSummary(inmate: self.inmates.objectAtIndex(indexPath.item) as! NSArray,parent: self));
return retVal;
}
private func getInmates(){
var totalInmates = 0;
var inmatesInArea = 0;
var inmatesOutOfArea = 0;
self.inmates = [];
do{
let tmpInmates : NSMutableArray = [];
let rs = try self.database.executeQuery("SELECT * FROM inmates WHERE podName='"+self.configuration.currentArea+"' ORDER BY suicideBlue DESC", values: nil)
while rs.next() {
let rowData : NSMutableArray = []
let colCount = Int(rs.columnCount()) as Int;
for i in 0 ..< colCount {
rowData.addObject(rs.objectForColumnName(rs.columnNameForIndex(Int32(i))));
}
if rowData[7] as! String == "" {
inmatesInArea = inmatesInArea + 1;
}else{
inmatesOutOfArea = inmatesOutOfArea + 1;
}
totalInmates = totalInmates + 1;
tmpInmates.addObject(rowData);
}
self.inmates = tmpInmates;
} catch {
CLSLogv("Error while loading inmates in main screen \(error)%#", getVaList(["three"]));
}
let barText = String(inmatesInArea) + "/" + String(inmatesOutOfArea) + " (" + String(totalInmates) + ")";
let buttonBack: UIButton = UIButton(type: UIButtonType.Custom) as UIButton
buttonBack.frame = CGRectMake(0, 0, 100, 40)
buttonBack.setTitle(barText, forState: .Normal);
let leftBarButtonItem: UIBarButtonItem = UIBarButtonItem(customView: buttonBack)
self.navigationItem.setLeftBarButtonItem(leftBarButtonItem, animated: false)
//self.database.close();
}
public func changePod(pod:String){
self.configuration.currentArea = pod;
self.titleView.text = pod + " ▼";
self.getInmates();
self.collectionView.reloadData();
}
public func refreshDAta(){
self.database.close();
self.database.open();
let inmateFetcer = InmateFetcher();
inmateFetcer.getUpdates();
self.getInmates();
//self.inmates = [];
self.collectionView.reloadData();
NSLog("DATA REFRESHED");
}
override public func preferredStatusBarStyle() -> UIStatusBarStyle {
return UIStatusBarStyle.LightContent
}
}
And here is the code for the views within the cells:
import Foundation
import UIKit
public class InmateSummary : UIView, UIPopoverPresentationControllerDelegate{
public var inmate:NSArray;
private var screenSize : CGRect = UIScreen.mainScreen().bounds;
private var configuration = Configuration.sharedInstance;
private var parent : podCollectionView;
init(inmate:NSArray, parent:podCollectionView){
let cellHeight = screenSize.height * 0.295;
let cellWidth = screenSize.width * 0.295;
self.inmate = inmate;
self.parent = parent;
super.init(frame: CGRect(x: 0, y: 0, width: cellWidth, height: cellHeight));
let aSelector : Selector = #selector(InmateSummary.inmateClicked(_:));
let tapGesture = UITapGestureRecognizer(target: self, action: aSelector);
tapGesture.numberOfTapsRequired = 1;
self.addGestureRecognizer(tapGesture);
self.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1);
let topView = UIView(frame: CGRect(x: 0, y: 0, width: cellWidth, height: cellHeight*0.10));
topView.backgroundColor = UIColor.grayColor();
self.addSubview(topView);
let cellLab = UILabel(frame: CGRect(x: 0, y: 0, width: cellWidth, height: cellHeight*0.10));
cellLab.text = self.inmate[3] as? String;
cellLab.textColor = UIColor.whiteColor();
cellLab.font = UIFont.init(name:"Arial-BoldMT", size: 30);
cellLab.textAlignment = NSTextAlignment.Center;
self.addSubview(cellLab);
let sexLab = UILabel(frame: CGRect(x: cellWidth*0.75, y: 0, width: cellWidth*0.25, height: cellHeight*0.10));
sexLab.text = self.inmate[1] as? String
sexLab.textColor = UIColor.whiteColor();
sexLab.textAlignment = NSTextAlignment.Center;
self.addSubview(sexLab);
let greyView = UIView(frame: CGRect(x: 0, y: cellHeight*0.10, width: cellWidth, height: cellHeight*0.70));
greyView.backgroundColor = UIColor.grayColor();
self.addSubview(greyView);
if self.inmate[7] as? String == "" {
greyView.backgroundColor = UIColor.blackColor();
}
let eventLab = UILabel(frame: CGRect(x: cellWidth*0.75, y: 0, width: cellWidth*0.25, height: cellHeight*0.10));
eventLab.text = self.inmate[7] as? String;
eventLab.textColor = UIColor.whiteColor();
eventLab.textAlignment = NSTextAlignment.Center;
eventLab.adjustsFontSizeToFitWidth = true;
greyView.addSubview(eventLab);
let classLab = UILabel(frame: CGRect(x: 0, y: 0, width: cellWidth*0.25, height: cellHeight*0.10));
classLab.text = self.inmate[4] as? String;
classLab.textAlignment = NSTextAlignment.Center;
classLab.font = UIFont.init(name:"Arial-BoldMT", size: 20);
classLab.adjustsFontSizeToFitWidth = true;
if(classLab.text == "RISK"){
classLab.textColor = UIColor.yellowColor();
}else if(classLab.text == "7CLS"){
classLab.textColor = UIColor(red: 1.0, green:0.5, blue:0.0, alpha:1);
}else if(classLab.text == "8MAX"){
classLab.textColor = UIColor(red:1.0, green:0.0, blue:0.0, alpha:0.7);
}else{
classLab.textColor = UIColor.whiteColor();
}
greyView.addSubview(classLab);
var mugshot : UIImage = UIImage();
if (self.inmate[12] as? NSData) != nil {
mugshot = UIImage(data: (self.inmate[12] as? NSData)!)!;
}else{
NSLog("Inmate without mugshot: %#",self.inmate[2] as! String);
}
let mugshotView = UIImageView(frame: CGRect(x: cellWidth*0.16, y: cellHeight*0.09, width: cellWidth*0.66, height: cellHeight*0.5));
mugshotView.image = mugshot;
greyView.addSubview(mugshotView);
let bookingNumber = UILabel(frame: CGRect(x: cellWidth*0.25, y: cellHeight*0.545, width: cellWidth*0.50, height: cellHeight*0.15));
bookingNumber.text = self.inmate[2] as? String;
bookingNumber.textColor = UIColor.whiteColor();
bookingNumber.textAlignment = NSTextAlignment.Center;
bookingNumber.font = UIFont.init(name:"Arial-BoldMT", size: 20);
bookingNumber.adjustsFontSizeToFitWidth = true;
greyView.addSubview(bookingNumber);
let nameView = UIView(frame: CGRect(x: 0, y: cellHeight*0.75, width: cellWidth, height: cellHeight*0.25));
self.addSubview(nameView);
let maxRed = UIView(frame: CGRect(x: 0, y: 0, width: cellWidth*0.33, height: cellHeight*0.115));
maxRed.backgroundColor = UIColor.blackColor();
if (self.inmate[15] as! NSNumber == 1) {
maxRed.backgroundColor = UIColor.redColor();
}
nameView.addSubview(maxRed);
let suBlue = UIView(frame: CGRect(x: cellWidth*0.33, y: 0, width: cellWidth*0.33, height: cellHeight*0.115));
suBlue.backgroundColor = UIColor.blackColor();
if (self.inmate[18] as! NSNumber == 1) {
let aSelector : Selector = #selector(InmateSummary.suicideClicked(_:));
let tapGesture = UITapGestureRecognizer(target: self, action: aSelector);
tapGesture.numberOfTapsRequired = 1;
nameView.addGestureRecognizer(tapGesture);
suBlue.backgroundColor = UIColor.blueColor();
}
nameView.addSubview(suBlue);
let segGreen = UIView(frame: CGRect(x: cellWidth*0.66, y: 0, width: cellWidth*0.33, height: cellHeight*0.115));
segGreen.backgroundColor = UIColor.blackColor();
if (self.inmate[5] as! NSNumber == 1) {
segGreen.backgroundColor = UIColor.greenColor();
}
nameView.addSubview(segGreen);
let nameLab = UILabel(frame: CGRect(x: cellWidth*0.025, y: cellHeight*0.12, width: cellWidth*0.95, height: cellHeight*0.05));
nameLab.text = self.inmate[0] as? String;
nameLab.textAlignment = NSTextAlignment.Center;
nameLab.textColor = UIColor.whiteColor();
nameLab.font = UIFont.init(name:"Arial-BoldMT", size: 20);
nameLab.adjustsFontSizeToFitWidth = true;
nameView.addSubview(nameLab);
if(self.inmate[18] as! NSNumber == 1){
//NSLog("Suicide: %#",self.inmate);
let checkLab = UILabel(frame: CGRect(x: 0, y: cellHeight*0.17, width: cellWidth, height: cellHeight*0.07));
let formatter = NSDateFormatter()
formatter.dateFormat = "MM/dd HH:mm"
let tmpDate = NSDate(timeIntervalSince1970: (self.inmate[19] as! NSString).doubleValue)
let dateString = formatter.stringFromDate(tmpDate);
checkLab.text = (self.inmate[22] as? String)! + " - " + dateString;
checkLab.textAlignment = NSTextAlignment.Center;
checkLab.textColor = UIColor.whiteColor();
checkLab.font = UIFont.init(name:"Arial", size: 18);
checkLab.adjustsFontSizeToFitWidth = true;
nameView.addSubview(checkLab);
}
if self.inmate[1] as? String == "F" {
nameView.backgroundColor = UIColor.purpleColor();
}
self.layer.cornerRadius = 5;
self.layer.borderColor = UIColor.grayColor().CGColor;
self.layer.borderWidth = 1;
self.layer.masksToBounds = true;
}
func suicideClicked(sender:UITapGestureRecognizer!){
let podsChooser = ActivityChooser(parentView:self);
podsChooser.modalPresentationStyle = .Popover;
let popoverMenuViewController = podsChooser.popoverPresentationController
popoverMenuViewController!.permittedArrowDirections = .Any
popoverMenuViewController!.delegate = self
popoverMenuViewController!.sourceView = sender.view;
self.parent.presentViewController(
podsChooser,
animated: true,
completion: nil)
}
func inmateClicked (sender:InmateSummary){
NSLog("Inmate Clicked");
let vc = SingleInmateDisplay(inmate: inmate);
self.parent.navigationController!.pushViewController(vc,animated:false);
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

How do i position my footerView so that it is on the bottom of the UITableView?

I want the footerView to be displayed on the bottom of the screen. How do i make it so that the footerView is located at the bottom and when it is tapped, the key board is appeared? Any help would be appreciated. Thanks!
protocol CommentsTableViewControllerDelegate: class {
func pop()
func reloadComments()
}
class CommentsTableViewController: UITableViewController, UITextViewDelegate, UITableViewDataSource{
weak var delegate: CommentsTableViewControllerDelegate?
var commentView: UITextView?
var footerView: UIView?
var contentHeight: CGFloat = 0
var comments: [String]?
var postObject: PFObject! {
didSet {
println(postObject)
}
}
private var myComments: [PFObject]? {
didSet {
tableView.reloadData()
}
}
private var header_label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
self.edgesForExtendedLayout = UIRectEdge.None
println(postObject?.objectForKey("comments"))
NSNotificationCenter.defaultCenter().addObserver(self, selector: "handlePostingComment:", name: postCommentNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleComments:", name: queryCommentNotification, object: nil)
// Query for comments
Downloader.sharedDownloader.queryForComments(postObject)
header_label = UILabel(frame: .zeroRect)
header_label.text = postObject["post"] as? String
header_label.sizeToFit()
header_label.frame.origin = .zeroPoint
tableView.tableHeaderView = header_label
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
/* Setup the datasource delegate */
tableView.delegate = self
tableView.dataSource = self
/* Setup the keyboard notifications */
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyBoardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyBoardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
/* Setup the contentInsets */
self.tableView.contentInset = UIEdgeInsetsZero
self.tableView.scrollIndicatorInsets = UIEdgeInsetsZero
self.edgesForExtendedLayout = UIRectEdge.None
/* Make sure the content doesn't go below tabbar/navbar */
self.extendedLayoutIncludesOpaqueBars = true
self.automaticallyAdjustsScrollViewInsets = false
if(postObject?.objectForKey("comments") != nil) {
comments = postObject?.objectForKey("comments") as? [String]
}
println(postObject)
println(postObject?.objectForKey("text"))
}
func handleComments(notification: NSNotification) {
let comments = notification.object as? [PFObject]
if let comments = comments {
myComments = comments
}
}
func handlePostingComment(notification: NSNotification) {
if let success = notification.object as? Bool {
if success {
delegate?.reloadComments()
} else {
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return myComments?.count ?? 0
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath) as! UITableViewCell
// Configure the cell...
if let myComments = myComments {
let comment = myComments[indexPath.row]
cell.textLabel?.text = comment["text"] as? String
}
return cell
}
func keyBoardWillShow(notification: NSNotification) {
var info:NSDictionary = notification.userInfo!
var keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()
var keyboardHeight:CGFloat = keyboardSize.height - 40
var animationDuration:CGFloat = info[UIKeyboardAnimationDurationUserInfoKey] as! CGFloat
var contentInsets: UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardHeight, 0.0);
self.tableView.contentInset = contentInsets
self.tableView.scrollIndicatorInsets = contentInsets
}
func keyBoardWillHide(notification: NSNotification) {
self.tableView.contentInset = UIEdgeInsetsZero
self.tableView.scrollIndicatorInsets = UIEdgeInsetsZero
}
override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
if self.footerView != nil {
return self.footerView!.bounds.height
}
return 50
}
override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
footerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.width, height: 100))
footerView?.backgroundColor = UIColor(red: 243.0/255, green: 243.0/255, blue: 243.0/255, alpha: 1)
commentView = UITextView(frame: CGRect(x: 10, y: 5, width: tableView.bounds.width - 80 , height: 30))
commentView?.backgroundColor = UIColor.whiteColor()
commentView?.textContainerInset = UIEdgeInsetsMake(5, 5, 5, 5)
commentView?.layer.cornerRadius = 2
commentView?.scrollsToTop = true
footerView?.addSubview(commentView!)
let button = UIButton(frame: CGRect(x: tableView.bounds.width - 65, y: 10, width: 60 , height: 30))
button.setTitle("Reply", forState: UIControlState.Normal)
button.backgroundColor = UIColor(red: 155.0/255, green: 189.0/255, blue: 113.0/255, alpha: 1)
button.layer.cornerRadius = 5
button.addTarget(self, action: "reply", forControlEvents: UIControlEvents.TouchUpInside)
footerView?.addSubview(button)
commentView?.delegate = self
return footerView
}
func textViewDidChange(textView: UITextView) {
if (contentHeight == 0) {
contentHeight = commentView!.contentSize.height
}
if(commentView!.contentSize.height != contentHeight && commentView!.contentSize.height > footerView!.bounds.height) {
UIView.animateWithDuration(0.2, animations: { () -> Void in
let myview = self.footerView
println(self.commentView!.contentSize.height)
println(self.commentView?.font.lineHeight)
let newHeight : CGFloat = self.commentView!.font.lineHeight
let myFrame = CGRect(x: myview!.frame.minX, y: myview!.frame.minY - newHeight , width: myview!.bounds.width, height: newHeight + myview!.bounds.height)
myview?.frame = myFrame
let mycommview = self.commentView
let newCommHeight : CGFloat = self.commentView!.contentSize.height
let myCommFrame = CGRect(x: mycommview!.frame.minX, y: mycommview!.frame.minY, width: mycommview!.bounds.width, height: newCommHeight)
mycommview?.frame = myCommFrame
self.commentView = mycommview
self.footerView = myview
for item in self.footerView!.subviews {
if(item.isKindOfClass(UIButton.self)){
let button = item as! UIButton
let newY = self.footerView!.bounds.height / 2 - button.bounds.height / 2
let buttonFrame = CGRect(x: button.frame.minX, y: newY , width: button.bounds.width, height : button.bounds.height)
button.frame = buttonFrame
}
}
})
println(self.footerView?.frame)
println(self.commentView?.frame)
contentHeight = commentView!.contentSize.height
}
}
func reply() {
println(commentView?.text)
Downloader.sharedDownloader.postingAComment(commentView!.text, post: postObject)
commentView!.text = ""
}
First create footer view.
func createFooterView() -> UIView {
self.footerView = UIView(frame: CGRect(x: 0, y: 0, width: self.tblView.bounds.width, height: 100))
self.footerView?.backgroundColor = UIColor(red: 243.0/255, green: 243.0/255, blue: 243.0/255, alpha: 1)
self.commentView = UITextView(frame: CGRect(x: 10, y: 5, width: self.tblView.bounds.width - 80 , height: 30))
self.commentView?.delegate = self
self.commentView?.backgroundColor = UIColor.whiteColor()
self.commentView?.textContainerInset = UIEdgeInsetsMake(5, 5, 5, 5)
self.commentView?.layer.cornerRadius = 2
self.commentView?.scrollsToTop = true
self.footerView?.addSubview(self.commentView!)
let button = UIButton(frame: CGRect(x: self.tblView.bounds.width - 65, y: 10, width: 60 , height: 30))
button.setTitle("Reply", forState: UIControlState.Normal)
button.backgroundColor = UIColor(red: 155.0/255, green: 189.0/255, blue: 113.0/255, alpha: 1)
button.layer.cornerRadius = 5
button.addTarget(self, action: "reply", forControlEvents: UIControlEvents.TouchUpInside)
self.footerView?.addSubview(button)
return self.footerView
}
And in viewDidLoad()
self.tblView.tableFooterView = self.createFooterView();

Programmatic implementation of iCarousel Swift example

Here is a Storyboard example of iCarousel implementation in Swift: https://github.com/nicklockwood/iCarousel/tree/master/Examples/Swift%20Example
Particularly this page:
https://github.com/nicklockwood/iCarousel/blob/master/Examples/Swift%20Example/SwiftExample/ViewController.swift
Can somebody guide me how to implement it programmatically (without Storyboards)?
swift 3
private let HEIGHT_CAROUSEL: CGFloat = 100
private func initiCarousel() {
let carousel = iCarousel(frame: CGRect(x: 0, y: view.frame.height - HEIGHT_CAROUSEL, width: view.frame.width, height: HEIGHT_CAROUSEL))
carousel.delegate = self
carousel.dataSource = self
view.addSubview(carousel)
}
// MARK: - iCarouselDelegate
extension YourNameViewController: iCarouselDataSource, iCarouselDelegate {
func numberOfItems(in carousel: iCarousel) -> Int {
return arrr.count ?? 0
}
func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {
var itemView: UIImageView
itemView = UIImageView(frame: CGRect(x: 0, y: 0, width: HEIGHT_CAROUSEL, height: HEIGHT_CAROUSEL))
itemView.contentMode = .scaleAspectFill
if let image = url {
itemView.setImageWithIndicator(imageUrl: image)
}
return itemView
}
func carousel(_ carousel: iCarousel, valueFor option: iCarouselOption, withDefault value: CGFloat) -> CGFloat {
if (option == .spacing) {
return value * 1.1
}
return value
}
}
Use following code to implement iCarousel programmatically using swift:
override func viewDidLoad()
{
super.viewDidLoad()
carousel = iCarousel(frame: CGRectMake(0, 0, 200, 200))
carousel.center = view.center
carousel.dataSource = self
carousel.delegate = self
}

Resources