UITableView cell height for iOS 7.1.2 - ios

I'm trying to adapt the height of the cells of a UITableView in swift programmatically for iOS 7.1.2 (sadly got asked for it.). There are no issues for all iOS versions after 7 but as it is the UITableView displays with a height of 1..
Here is my code (I made the interface programmatically as I literally HATE the design interface of Xcode and don't use AutoLayout).
Any ideas?
import UIKit
class ChemieListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {
private var imageView = UIImageView ()
private var bg_image = UIImage()
private var titre = UILabel()
private var button_Back = UIButton()
var navigationBar = UIView()
var items:[String] = ["1","A","B","C","D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
var lbl_suche_by_txt = UILabel()
var text_search = UITextField()
var alph_list = UITableView()
var lbl_such_by_alph = UILabel()
var scroll: UIScrollView!
var scroll_h: CGFloat = CGFloat(0)
private var waitView = UIView()
private var loading_view = UIImageView()
private var loading_text = UILabel()
private var has_started = false
private var bg_infro = UIView()
private var txt_info = UILabel()
private var options_pics = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
//---------------------Scroll View-----------------------------
self.scroll = UIScrollView()
scroll_h = 0
//----------------------Wait screen---------------------------
waitView.backgroundColor = UIColor.blackColor()
waitView.alpha = 0.8
let ScreenSize: CGRect = UIScreen.mainScreen().bounds
loading_text.text = NSLocalizedString("wait", comment: " ")
loading_text.textColor = UIColor(white: 1, alpha: 1)
loading_text.textAlignment = .Center
loading_text.backgroundColor = UIColor(white: 1, alpha: 0)
//----------------------Navigation Bar-------------------------
navigationBar.backgroundColor = GetColorFromHex(0x2139D2)
//----------------------Back Button----------------------------
var back_img: UIImage? = UIImage(named: "back")
if (back_img == nil){
back_img = UIImage(named: "back.png")
}
button_Back.translatesAutoresizingMaskIntoConstraints = false
button_Back.addTarget(self, action: #selector(ProduktViewController.button_back_Pressed), forControlEvents: .TouchDown)
button_Back.setBackgroundImage(back_img, forState: UIControlState.Normal)
button_Back.contentMode = UIViewContentMode.ScaleAspectFit
self.view.addSubview(button_Back)
//----------------------Background Image-----------------------
//Background Image
var BGImage: UIImage? = UIImage(named: "background")
if(BGImage == nil){
BGImage = UIImage(named: "background.jpg")
}
bg_image = BGImage!
imageView = UIImageView(frame: self.view.bounds)
imageView.image = bg_image
imageView.clipsToBounds = true
self.view.addSubview(imageView)
self.view.sendSubviewToBack(imageView)
//------------------------Title------------------------------------
let largeur_title = ScreenSize.width - button_Back.frame.width - 30
titre.adjustsFontSizeToFitWidth = true
titre.text = NSLocalizedString("liste", comment: " ")
titre.textColor = UIColor.whiteColor()
titre.frame = CGRect (x: button_Back.frame.origin.x + button_Back.frame.width + 10, y: button_Back.frame.origin.y, width: largeur_title, height: 50)
titre.textAlignment = .Center
//---------------------txt For input search-----------------------
lbl_suche_by_txt.text = NSLocalizedString("search_field", comment: "")
lbl_suche_by_txt.textAlignment = .Center
lbl_suche_by_txt.backgroundColor = UIColor(white: 1, alpha: 0.5)
//---------------------TextField input---------------------------
text_search.delegate = self
text_search.resignFirstResponder()
text_search.textAlignment = .Center
text_search.backgroundColor = UIColor.whiteColor()
text_search.placeholder = NSLocalizedString("search_here", comment: "")
text_search.keyboardType = UIKeyboardType.Default
text_search.returnKeyType = UIReturnKeyType.Done
//---------------------txt For alphabetical search-----------------------
lbl_such_by_alph.text = NSLocalizedString("search_list", comment: "")
lbl_such_by_alph.textAlignment = .Center
lbl_such_by_alph.backgroundColor = UIColor(white: 1, alpha: 0.5)
//---------------------Table View Alphabet-------------------------------
alph_list.delegate = self
alph_list.dataSource = self
alph_list.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
alph_list.estimatedRowHeight = 50.0
alph_list.rowHeight = UITableViewAutomaticDimension
//---------------------BG Info Rights------------------------------------
bg_infro.backgroundColor = UIColor(white: 1, alpha: 0.5)
txt_info.text = NSLocalizedString("rights", comment: "")
txt_info.numberOfLines = 0
txt_info.textColor = UIColor.blackColor()
let orient = UIApplication.sharedApplication().statusBarOrientation
switch orient{
case .Portrait:
self.setContstraintsPortrait()
break
default:
self.setContstraintsLandscape()
break
}
navigationBar.addSubview(button_Back)
navigationBar.addSubview(titre)
self.scroll.addSubview(lbl_suche_by_txt)
self.scroll.addSubview(text_search)
self.scroll.addSubview(lbl_such_by_alph)
self.scroll.addSubview(alph_list)
self.scroll.addSubview(bg_infro)
self.scroll.addSubview(txt_info)
waitView.addSubview(loading_text)
waitView.hidden = true
self.scroll.addSubview(waitView)
self.view.addSubview(scroll)
self.view.addSubview(navigationBar)
}
override func viewDidAppear(animated: Bool) {
alph_list.reloadData()
var table_height:CGFloat = 0
//----------------Init Values----------------------
let ScreenSize: CGRect = UIScreen.mainScreen().bounds
let w = ScreenSize.width
for index in 0...items.count - 1 {
let IndexPath = NSIndexPath(forRow:index, inSection:0)
let cell = alph_list.cellForRowAtIndexPath(IndexPath)
if cell != nil {
let cell: UITableViewCell = alph_list.cellForRowAtIndexPath(IndexPath)!
cell.textLabel?.lineBreakMode = .ByWordWrapping
cell.textLabel?.numberOfLines = 0
print(cell.textLabel?.text)
alph_list.endUpdates()
table_height += alph_list.cellForRowAtIndexPath(IndexPath)!.frame.height
print(table_height)
if #available(iOS 8, *){
table_height += alph_list.cellForRowAtIndexPath(IndexPath)!.frame.height
}else{
print(table_height)
}
}
}
alph_list.frame = CGRect (x: button_Back.frame.origin.x, y: lbl_such_by_alph.frame.origin.y + lbl_such_by_alph.frame.height + 10, width: w - 20 , height: 26*45)
alph_list.contentSize = CGSizeMake(200, (26*45))
//-------------BG info rights--------------------------
bg_infro.frame = CGRect(x: button_Back.frame.origin.x, y: alph_list.frame.origin.y + alph_list.frame.height + 10 , width: w - 20, height: 220)
//-------------BG txt rights--------------------------
txt_info.frame = CGRect(x: button_Back.frame.origin.x + 10, y: bg_infro.frame.origin.y + 10 , width: w - 40, height: 250)
scroll_h = bg_infro.frame.origin.y + bg_infro.frame.height + 30
self.scroll.contentSize = CGSizeMake(w, scroll_h)
has_started = true
}
func setContstraintsLandscape(){
print("portrait func beständigkeitsliste")
//----------------Scroll View-----------------------
scroll.frame = UIScreen.mainScreen().bounds
//----------------Init Values-----------------------
let ScreenSize: CGRect = UIScreen.mainScreen().bounds
let w = ScreenSize.width
let h = ScreenSize.height
let zero: CGFloat = 0
//-------------Background---------------------------
imageView.removeFromSuperview()
imageView.translatesAutoresizingMaskIntoConstraints = true
imageView.frame = CGRectMake(zero,zero, w, h)
imageView.image = bg_image
self.view.addSubview(imageView)
self.view.sendSubviewToBack(imageView)
//---------------Navigation Bar--------------------
navigationBar.frame = CGRect(x: 0, y: 0, width: w, height: 60)
//--------------Button Back-------------------------
button_Back.translatesAutoresizingMaskIntoConstraints = true
button_Back.frame = CGRectMake(10, 10, 50, 50)
//--------------Title-------------------------------
titre.frame = CGRect (x: 0, y: button_Back.frame.origin.y, width: w, height: 50)
//-------------Text Search by Text------------------
lbl_suche_by_txt.frame = CGRect(x: button_Back.frame.origin.x, y: button_Back.frame.origin.y + button_Back.frame.height + 10 , width: w - 20, height: 40)
//-------------Text Field Search by Text------------------
text_search.frame = CGRect(x: button_Back.frame.origin.x, y: lbl_suche_by_txt.frame.origin.y + lbl_suche_by_txt.frame.height + 10 , width: w - 20, height: 40)
//-------------Text Search by Alphabet------------------
lbl_such_by_alph.frame = CGRect(x: button_Back.frame.origin.x, y: text_search.frame.origin.y + text_search.frame.height + 30 , width: w - 20, height: 40)
//-------------List Alphabet------------------
var table_height: CGFloat = 0
if(has_started == false){
table_height = CGFloat(items.count * 50)
}else{
for index in 0...items.count - 1 {
let IndexPath = NSIndexPath(forRow:index, inSection:0)
let cell = alph_list.cellForRowAtIndexPath(IndexPath)
if cell != nil {
table_height += alph_list.cellForRowAtIndexPath(IndexPath)!.frame.height
}
}
}
alph_list.frame = CGRect(x: button_Back.frame.origin.x, y: lbl_such_by_alph.frame.origin.y + lbl_such_by_alph.frame.height + 10 , width: w - 20, height: table_height)
//-------------BG info rights--------------------------
bg_infro.frame = CGRect(x: button_Back.frame.origin.x, y: alph_list.frame.origin.y + alph_list.frame.height + 10 , width: w - 20, height: 220)
//-------------BG txt rights--------------------------
txt_info.frame = CGRect(x: button_Back.frame.origin.x + 10, y: bg_infro.frame.origin.y + 20 , width: w - 40, height: 250)
//-------------Scroll View Size-------------------------------
scroll_h = alph_list.frame.origin.y + table_height + 30
self.scroll.contentSize = CGSizeMake(ScreenSize.width, scroll_h)
//----------------Wait Screen-----------------------
waitView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.scroll.frame.height)
loading_text.frame = CGRect(x: (ScreenSize.width - 150) / 2, y: (ScreenSize.height - 100) / 2, width: 150, height: 100)
}
func setContstraintsPortrait(){
print("portrait func liste chemie")
//----------------Scroll View-----------------------
scroll.frame = UIScreen.mainScreen().bounds
//----------------Init Values-----------------------
let ScreenSize: CGRect = UIScreen.mainScreen().bounds
let w = ScreenSize.width
let h = ScreenSize.height
let zero: CGFloat = 0
//--------------------------------------------------
imageView.removeFromSuperview()
imageView.translatesAutoresizingMaskIntoConstraints = true
imageView.frame = CGRectMake(zero,zero, w, h)
imageView.image = bg_image
self.view.addSubview(imageView)
self.view.sendSubviewToBack(imageView)
//---------------Navigation Bar---------------------
navigationBar.frame = CGRect(x: 0, y: 0, width: w, height: 80)
//--------------Button Back-------------------------
button_Back.translatesAutoresizingMaskIntoConstraints = true
button_Back.frame = CGRectMake(10, 30, 50, 50)
//--------------Title-------------------------------
titre.frame = CGRect (x: 0, y: button_Back.frame.origin.y, width: w, height: 50)
//-------------Text Search by Text------------------
lbl_suche_by_txt.frame = CGRect(x: button_Back.frame.origin.x, y: button_Back.frame.origin.y + button_Back.frame.height + 10 , width: w - 20, height: 40)
//-------------Text Field Search by Text------------------
text_search.frame = CGRect(x: button_Back.frame.origin.x, y: lbl_suche_by_txt.frame.origin.y + lbl_suche_by_txt.frame.height + 10 , width: w - 20, height: 40)
//-------------Text Search by Alphabet------------------
lbl_such_by_alph.frame = CGRect(x: button_Back.frame.origin.x, y: text_search.frame.origin.y + text_search.frame.height + 30 , width: w - 20, height: 40)
//-------------List Alphabet------------------
var table_height: CGFloat = 0
if(has_started == false){
table_height = CGFloat(items.count * 50)
}else{
for index in 0...items.count - 1 {
let IndexPath = NSIndexPath(forRow:index, inSection:0)
let cell = alph_list.cellForRowAtIndexPath(IndexPath)
if cell != nil {
table_height += alph_list.cellForRowAtIndexPath(IndexPath)!.frame.height
}
}
}
alph_list.frame = CGRect(x: button_Back.frame.origin.x, y: lbl_such_by_alph.frame.origin.y + lbl_such_by_alph.frame.height + 10 , width: w - 20, height: table_height)
alph_list.contentSize = CGSizeMake(w-20, table_height)
print(table_height)
//-------------BG info rights--------------------------
bg_infro.frame = CGRect(x: button_Back.frame.origin.x, y: alph_list.frame.origin.y + alph_list.frame.height + 10 , width: w - 20, height: 220)
//-------------BG txt rights--------------------------
txt_info.frame = CGRect(x: button_Back.frame.origin.x + 10, y: bg_infro.frame.origin.y + 20 , width: w - 40, height: 250)
//-------------Scroll View Size-------------------------------
scroll_h = alph_list.frame.origin.y + table_height + 30
self.scroll.contentSize = CGSizeMake(w, scroll_h)
//----------------Wait Screen-----------------------
waitView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.scroll.frame.height)
loading_text.frame = CGRect(x: (w - 150) / 2, y: (h - 100) / 2, width: 150, height: 100)
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
text_search.resignFirstResponder()
print("Oki")
let listresultview: ListResultsViewController = ListResultsViewController(imageURL: text_search.text)
self.presentViewController(listresultview, animated:true, completion: nil)
return true
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell
cell.textLabel?.text = self.items[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
waitView.frame = CGRect(x: 0, y: scroll.bounds.origin.y, width: self.view.frame.width, height: self.scroll.frame.height)
loading_text.frame = CGRect(x: (UIScreen.mainScreen().bounds.width - 150) / 2, y: (UIScreen.mainScreen().bounds.height - 100) / 2, width: 150, height: 100)
self.waitView.hidden = false
dispatch_async(dispatch_get_main_queue()) {
self.loadListResultView(indexPath.row)
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
text_search.resignFirstResponder()
self.view.endEditing(true)
}
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}

You should create a custom UITableViewCell.
Adding the constraints to UITableViewCell contentView resolved it for me.

Related

Set section header padding of tableview for second header in old iOS

I'm setting header padding for my tableview in iOS older than 15 explicitly. This works for the top first header (section 1 of tableview), but doesn't work for second header (section 2 of tableview). How could I set it for the second also?
Is there something like tableView.tableHeaderView[2] or tableView.tableHeaderView.second ?
In code its the place with commented-out places.
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if tableView.tag == 2 {
if section == 0 {
if self.tableView(tableView, numberOfRowsInSection: section) > 0 {
let view = UIView()
let imageView = UIImageView(image: UIImage(named: "Logo_face"))
if #unavailable(iOS 15.0) {
imageView.frame = CGRect(x: 10, y: 20, width: 25, height: 25)
} else {
imageView.frame = CGRect(x: 10, y: 0, width: 25, height: 25)
}
view.addSubview(imageView)
let label = UILabel()
label.text = "店舗"
label.frame = CGRect(x: 40, y: 0, width: 200, height: 25)
view.addSubview(label)
if #unavailable(iOS 15.0) { //setting here; works
let padding: CGFloat = 20
view.frame = CGRect(x: view.frame.origin.x - padding,
y: view.frame.origin.y - padding,
width: view.frame.width + 2 * padding,
height: view.frame.height + 2 * padding)
tableView.tableHeaderView = view // this is good
label.frame = CGRect(x: 40, y: 20, width: 200, height: 25)
}
return view
}
} else {
if self.tableView(tableView, numberOfRowsInSection: section) > 0 {
let view = UIView()
let imageView = UIImageView(image: UIImage(named: "pinIconForSearch"))
if #unavailable(iOS 15.0) {
imageView.frame = CGRect(x: 10, y: 20, width: 25, height: 25)
} else {
imageView.frame = CGRect(x: 10, y: 0, width: 25, height: 25)
}
view.addSubview(imageView)
let label = UILabel()
label.text = "検索候補"
label.frame = CGRect(x: 40, y: 0, width: 200, height: 25)
view.addSubview(label)
if #unavailable(iOS 15.0) { //setting here doesnt work
let padding: CGFloat = 20
view.frame = CGRect(x: view.frame.origin.x - padding,
y: view.frame.origin.y - padding,
width: view.frame.width + 2 * padding,
height: view.frame.height + 2 * padding)
tableView.tableHeaderView = view // this is not good
label.frame = CGRect(x: 40, y: 20, width: 200, height: 25)
}
return view
}
}
}
if #available(iOS 15.0, *) {
tableView.sectionHeaderTopPadding = 0.0
} else {}
return nil
}

UiViewController takes a long time to display

then I have a UIViewController class that is called instantiated and displayed by a method, the problem lies in the fact that the end of self.present the view is not completely displayed, but only a part is displayed, after about 5-6 seconds, the rest of the view is also displayed! How can I make the view appear completely at the end of self.present?
Swift Code:
///First Method (It's call nextviewcontroller
var ret: Bool = false
let c = c(c: self.c!)
c.crea(n: self.txtn.text!, completion: { result in
ret = result
})
if(ret == true)
{
let viewnext = NextViewController(c: c)
self.present(viewnext, animated: true, completion: { () in print("Done") })
}
//NextViewController.swift
class NextViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
//Oggetti Privati
private var TextViewDesc: UITextView = UITextView()
private var pickerviewStato: UIPickerView = UIPickerView()
private var labelnameINFO: UILabel = UILabel()
private var labelClientINFO: UILabel = UILabel()
private var labelfilINFO: UILabel = UILabel()
private var labelClient: UILabel = UILabel()
private var labelfil: UILabel = UILabel()
private var buttonris: UIButton = UIButton(type: .custom)
private var buttonKilometri = UIButton(type: .custom)
private var buttonfield = UIButton(type: .custom)
private var buttonprint = UIButton(type: .custom)
private var buttonReturn = UIButton()
private let statoCantiere = ["InCorso", "Chiuso", "Lavoro terminato inserire bolle"]
private var immagineStatoCantiere: UIImageView = UIImageView()
private let DistLtoR: CGFloat = 130
private let DistRtoL: CGFloat = 50
//Oggetti Pubblici
var c: c
init(c: c) {
c = c
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
hideKeyboardWhenTappedAround()
self.view.backgroundColor = UIColor.white
initView()
}
func initView() {
labelnameINFO = UILabel(frame: CGRect(x: 20, y: 60, width: 120, height: 21))
labelnameINFO.text = "Cantiere:"
labelnameINFO.font = UIFont(name: "HelveticaNeue-Bold", size: 16.0)
self.view.addSubview(labelnameINFO)
let labelname = UILabel(frame: CGRect(x: 100, y: 60, width: 200, height: 21))
labelname.text = CantiereInterno.Getname()
self.view.addSubview(labelname)
labelClientINFO = UILabel(frame: CGRect(x: 20, y: 100, width: 130, height: 21))
labelClientINFO.text = "Ragione Sociale:"
labelClientINFO.font = UIFont(name: "HelveticaNeue-Bold", size: 16.0)
self.view.addSubview(labelClientINFO)
labelClient = UILabel(frame: CGRect(x: 180, y: 100, width: 180, height: 21))
labelClient.text = c.GetRagioneSociale()
self.view.addSubview(labelClient)
//Configurazioe label fil
labelfilINFO = UILabel(frame: CGRect(x: 20, y: 140, width: 130, height: 21))
labelfilINFO.text = "fil:"
labelfilINFO.font = UIFont(name: "HelveticaNeue-Bold", size: 16.0)
self.view.addSubview(labelfilINFO)
labelfil = UILabel(frame: CGRect(x: 100, y: 140, width: 180, height: 21))
labelfil.text = c.Getfil()
self.view.addSubview(labelfil)
let labelStato = UILabel(frame: CGRect(x: 20, y: 190, width: 130, height: 21))
labelStato.text = "Stato:"
labelStato.font = UIFont(name: "HelveticaNeue-Bold", size: 16.0)
self.view.addSubview(labelStato)
self.pickerviewStato = UIPickerView(frame: CGRect(x: 90, y: 180, width: 160, height: 50))
self.pickerviewStato.delegate = self as UIPickerViewDelegate
self.pickerviewStato.dataSource = self as UIPickerViewDataSource
self.pickerviewStato.backgroundColor = UIColor.gray
self.view.addSubview(pickerviewStato)
immagineStatoCantiere = UIImageView(frame: CGRect(x: (self.view.frame.width / 2) + 50, y: 180, width: 50, height: 50));
immagineStatoCantiere.image = UIImage(named: "InCorso.png")
immagineStatoCantiere.backgroundColor = UIColor.gray
self.view.addSubview(immagineStatoCantiere)
self.TextViewDesc = UITextView(frame: CGRect(x: 0, y: 250, width: self.view.frame.width, height: 80))
self.automaticallyAdjustsScrollViewInsets = false
TextViewDesc.font = UIFont(name: "HelveticaNeue-Bold", size: 18.0)
TextViewDesc.textAlignment = NSTextAlignment.justified
TextViewDesc.textColor = UIColor.blue
TextViewDesc.isEditable = true
TextViewDesc.backgroundColor = UIColor.lightGray
TextViewDesc.text = c.GetDescrizioneEstesa()
self.view.addSubview(TextViewDesc)
buttonris.frame = CGRect(x: (self.view.frame.width / 2) - DistLtoR, y: 380, width: 100, height: 100)
buttonris.layer.cornerRadius = 0.5 * buttonris.bounds.size.width
buttonris.clipsToBounds = true
buttonris.setImage(UIImage(named: "risorse_umane.png"), for: .normal)
buttonris.backgroundColor = UIColor.lightGray
buttonris.addTarget(self, action: #selector(risbuttonAction), for: .touchUpInside)
self.view.addSubview(buttonris)
buttonKilometri.frame = CGRect(x: (self.view.frame.width / 2) + DistRtoL, y: 380, width: 100, height: 100)
buttonKilometri.layer.cornerRadius = 0.5 * buttonKilometri.bounds.size.width
buttonKilometri.clipsToBounds = true
buttonKilometri.setImage(UIImage(named: "kilometri.png"), for: .normal)
buttonKilometri.backgroundColor = UIColor.lightGray
buttonKilometri.addTarget(self, action: #selector(KilometributtonAction), for: .touchUpInside)
self.view.addSubview(buttonKilometri)
buttonfield.frame = CGRect(x: (self.view.frame.width / 2) - DistLtoR, y: 500, width: 100, height: 100)
buttonfield.layer.cornerRadius = 0.5 * buttonfield.bounds.size.width
buttonfield.clipsToBounds = true
buttonfield.setImage(UIImage(named: "field.png"), for: .normal)
buttonfield.backgroundColor = UIColor.lightGray
buttonfield.addTarget(self, action: #selector(fieldbuttonAction), for: .touchUpInside)
self.view.addSubview(buttonfield)
buttonprint.frame = CGRect(x: (self.view.frame.width / 2) + DistRtoL, y: 500, width: 100, height: 100)
buttonprint.layer.cornerRadius = 0.5 * buttonfield.bounds.size.width
buttonprint.clipsToBounds = true
buttonprint.setImage(UIImage(named: "print.png"), for: .normal)
buttonprint.backgroundColor = UIColor.lightGray
buttonprint.addTarget(self, action: #selector(RapportinobuttonAction), for: .touchUpInside)
self.view.addSubview(buttonprint)
buttonprint.isHidden = false
buttonReturn.frame = CGRect(x: (self.view.frame.width / 2), y: 600, width: 80, height: 80)
buttonReturn.backgroundColor = UIColor.white
buttonReturn.setImage(UIImage(named: "return.png"), for: .normal)
buttonReturn.setTitle("Go Home", for: .normal)
buttonReturn.addTarget(self, action: #selector(ReturnToHomebuttonAction), for: .touchUpInside)
self.view.addSubview(buttonReturn)
}
Call initView in viewwillappear:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if labelnameINFO == nil{
self.initView()
}
}

Adding lines using CGRect

I can draw two perpendicular lines using UIView. I make y in vertical line 0, but if I change y I will not get perpendicular lines. How can I improve code?
var scanlineRect = CGRect.zero
var scanlineStartY: CGFloat = 30
var scanlineStopY: CGFloat = 0
var topBottomMargin: CGFloat = 30
var scanLine: UIView = UIView()
var scanlineRect2 = CGRect.zero
var scanlineStartY2: CGFloat = 100
var scanlineStopY2: CGFloat = 0
var topBottomMargin2: CGFloat = 30
var scanLine2: UIView = UIView()
func drawLine(Rect: CGRect) {
if Rect == scanlineRect
{
self.view.addSubview(scanLine)
scanLine.backgroundColor = UIColor.gray
scanlineRect = CGRect(x: 15, y: self.view.frame.height/2, width: self.view.frame.width - 30, height: 5)
scanLine.frame = scanlineRect
scanLine.center = CGPoint(x: scanLine.center.x, y: self.view.frame.height/2)
scanLine.isHidden = false
}
else if Rect == scanlineRect2{
self.view.addSubview(scanLine2)
scanLine2.backgroundColor = UIColor.black
scanlineRect2 = CGRect(x: 350, y: 0, width: 5, height: self.view.frame.height/2)
scanLine2.frame = scanlineRect2
scanLine2.center = CGPoint(x: 350, y: scanLine2.center.y)
scanLine2.isHidden = false
}
}
override func viewDidLoad() {
super.viewDidLoad()
drawLine(Rect: scanlineRect)
drawLine(Rect: scanlineRect2)
}
When y of vertical line equals 0
When y of vertical line is NOT equals 0

How to make image weight and height fill screen in Swift Xcode?

I found a code to make slide in swift, but cant find, how to make the IMAGE fill the whole screen.
Could you help?
here is the screenshot of slider, and you will see the anchors I placed on it to show you, the whole screen.
and here is the code of it;
import UIKit
class OnboardingController: UIViewController, UIScrollViewDelegate {
let backgroundColor = UIColor(red: 241.0/255.0, green: 196.0/255.0, blue: 15.0/255.0, alpha: 1.0)
let slides = [
[ "image": "book4page1.png"],
[ "image": "book4page2.png"],
[ "image": "book4page3.png"],
]
let screen: CGRect = UIScreen.mainScreen().bounds
var scroll: UIScrollView?
var dots: UIPageControl?
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = backgroundColor
scroll = UIScrollView(frame: CGRect(x: 0.0, y: 0.0, width: screen.width, height: screen.height * 0.9))
scroll?.showsHorizontalScrollIndicator = false
scroll?.showsVerticalScrollIndicator = false
scroll?.pagingEnabled = true
view.addSubview(scroll!)
if (slides.count > 1) {
dots = UIPageControl(frame: CGRect(x: 0.0, y: screen.height * 0.875, width: screen.width, height: screen.height * 0.05))
dots?.numberOfPages = slides.count
view.addSubview(dots!)
}
for var i = 0; i < slides.count; ++i {
if let image = UIImage(named: slides[i]["image"]!) {
let imageView: UIImageView = UIImageView(frame: getFrame(image.size.width, iH: image.size.height, slide: i, offset: screen.height * 0.15))
imageView.image = image
scroll?.addSubview(imageView)
}
if let text = slides[i]["text"] {
let textView = UITextView(frame: CGRect(x: screen.width * 0.05 + CGFloat(i) * screen.width, y: screen.height * 0.745, width: screen.width * 0.9, height: 100.0))
textView.text = text
textView.editable = false
textView.selectable = false
textView.textAlignment = NSTextAlignment.Center
textView.font = UIFont.systemFontOfSize(20, weight: 0)
textView.textColor = UIColor.whiteColor()
textView.backgroundColor = UIColor.clearColor()
scroll?.addSubview(textView)
}
}
scroll?.contentSize = CGSizeMake(CGFloat(Int(screen.width) * slides.count), screen.height * 0.5)
scroll?.delegate = self
dots?.addTarget(self, action: Selector("swipe:"), forControlEvents: UIControlEvents.ValueChanged)
let closeButton = UIButton()
closeButton.frame = CGRect(x: screen.width - 70, y: 20, width: 60, height: 60)
closeButton.setTitle("Skip", forState: .Normal)
closeButton.setTitleColor(UIColor(red: 0.0/255.0, green: 0.0/255.0, blue: 0.0/255.0, alpha: 0.5), forState: .Normal)
closeButton.titleLabel!.font = UIFont.systemFontOfSize(16)
closeButton.addTarget(self, action: "pressed:", forControlEvents: .TouchUpInside)
view.addSubview(closeButton)
}
func pressed(sender: UIButton!) {
self.dismissViewControllerAnimated(true) { () -> Void in
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func getFrame (iW: CGFloat, iH: CGFloat, slide: Int, offset: CGFloat) -> CGRect {
let mH: CGFloat = screen.height * 0.50
let mW: CGFloat = screen.width
var h: CGFloat
var w: CGFloat
let r = iW / iH
if (r <= 1) {
h = min(mH, iH)
w = h * r
} else {
w = min(mW, iW)
h = w / r
}
return CGRectMake(
max(0, (mW - w) / 2) + CGFloat(slide) * screen.width,
max(0, (mH - h) / 2) + offset,
w,
h
)
}
func swipe(sender: AnyObject) -> () {
if let scrollView = scroll {
let x = CGFloat(dots!.currentPage) * scrollView.frame.size.width
scroll?.setContentOffset(CGPointMake(x, 0), animated: true)
}
}
func scrollViewDidEndDecelerating(scrollView: UIScrollView) -> () {
let pageNumber = round(scrollView.contentOffset.x / scrollView.frame.size.width)
dots!.currentPage = Int(pageNumber)
}
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return UIStatusBarStyle.LightContent
}
}
On your imageView set imageView.contentMode = UIViewContentMode.ScaleAspectFit
Depending on how you want it to scale you should modify the contentMode
of the UIImageView's.
In Objective-C you would do this (it'll be something similar in Swift):
UIImageView * iv = [UIImageView new];
iv.frame = scrollView.bounds;
iv.contentMode = UIViewContentModeScaleAspectFill;
iv.clipsToBounds = true;
iv.image = [UIImage imageNamed:#"image.jpg"];
[scrollView addSubview:iv];
The contentMode is the line you're looking for.

Move UITextView to bottom of ViewController programmatically?

I created the textView and Button(reply) programmatically and I want to move it down to the bottom of my ViewController. It's now being placed under the label(Joshyjosh) and it shouldn't do that. Is there something I need to add to my code or change so it can move to the bottom?
import UIKit
class DetailViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UITextViewDelegate {
var commentView: UITextView?
var footerView: UIView?
var contentHeight: CGFloat = 0
let FOOTERHEIGHT : CGFloat = 50;
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
if self.footerView != nil {
return self.footerView!.bounds.height
}
return FOOTERHEIGHT
}
func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
footerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.width, height: FOOTERHEIGHT))
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: 40))
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
print(self.commentView!.contentSize.height)
print(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
}
}
})
print(self.footerView?.frame)
print(self.commentView?.frame)
contentHeight = commentView!.contentSize.height
}
}

Resources