Adding lines using CGRect - ios

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

Related

macOS NSScrollView Not Scrolling Vertically

I am trying to add few views to see if the NSScrollView will scroll vertically but it is not doing anything.
private func configure2() {
var yOffset = 0
let scrollView = NSScrollView(frame: NSRect(x: 0, y: 0, width: 400, height: 900))
scrollView.hasVerticalScroller = true
for _ in 1...20 {
let v = NSView(frame: NSRect(x: 0, y: 0 + yOffset, width: 50, height: 20))
v.wantsLayer = true
v.layer?.backgroundColor = NSColor.red.cgColor
scrollView.addSubview(v)
yOffset += 40
}
scrollView.backgroundColor = NSColor.green
self.addSubview(scrollView)
}
I remember in UIKit I can set the contentSize property of UIScrollView but in macOS I cannot set contentSize.
You need to set either the documentView or contentView of your NSScrollView. Then, you'll add your subviews to that view.
private func configure2() {
var yOffset = 0
let scrollView = NSScrollView(frame: NSRect(x: 0, y: 0, width: 400, height: 900))
let documentView = NSView(frame: .zero)
scrollView.hasVerticalScroller = true
for _ in 1...20 {
let v = NSView(frame: NSRect(x: 0, y: 0 + yOffset, width: 50, height: 20))
v.wantsLayer = true
v.layer?.backgroundColor = NSColor.red.cgColor
documentView.addSubview(v)
yOffset += 40
}
print(yOffset)
documentView.frame = .init(x: 0, y: 0, width: 400, height: yOffset)
scrollView.documentView = documentView
scrollView.backgroundColor = NSColor.green
self.addSubview(scrollView)
}

How do you determine the drag and drop location in Swift 5?

I have mocked up a simple example of what I am trying to accomplish:
A ViewController contains 4 "drop zone" UIImageViews (e.g. dropZone1). A 5th UIImageView (playerCard) can be dragged and dropped onto any of the drop zones, but nowhere else.
I cannot figure out the way to determine which of the 4 drop zones is where the user has dragged and dropped the playerCard.
My thought was to set some sort of variable in dropInteraction canHandle and then use that in dropInteraction performDrop to take the appropriate action. But I can't figure out how to do it.
class ViewController: UIViewController {
let bounds = UIScreen.main.bounds
let imageViewWidth: CGFloat = 100
let imageViewHeight: CGFloat = 200
let inset: CGFloat = 40
var arrayDropZones = [DropZoneCard]()
var initialFrame: CGRect {
get {
return CGRect(x: bounds.width - imageViewWidth,
y: bounds.height - imageViewHeight,
width: imageViewWidth,
height: imageViewHeight
)
}
}
override func viewDidLoad() {
super.viewDidLoad()
addDropZones()
addNewCard()
}
}
extension ViewController {
func addDropZones() {
let dropZone1 = getDropZoneCard()
dropZone1.frame = CGRect(x: inset, y: inset, width: imageViewWidth, height: imageViewHeight)
let dropZone2 = getDropZoneCard()
let x = bounds.width - imageViewWidth - inset
dropZone2.frame = CGRect(x: x, y: inset, width: imageViewWidth, height: imageViewHeight)
let dropZone3 = getDropZoneCard()
let y = inset + imageViewHeight + inset
dropZone3.frame = CGRect(x: inset, y: y, width: imageViewWidth, height: imageViewHeight)
let dropZone4 = getDropZoneCard()
dropZone4.frame = CGRect(x: x, y: y, width: imageViewWidth, height: imageViewHeight)
[dropZone1, dropZone2, dropZone3, dropZone4].forEach {
view.addSubview($0)
self.arrayDropZones.append($0)
}
}
func getNewCard() -> UIImageView {
let imageView = UIImageView()
imageView.isUserInteractionEnabled = true
imageView.backgroundColor = .green
imageView.frame = initialFrame
let panGesture = UIPanGestureRecognizer(target: self, action:(#selector(handleGesture(_:))))
imageView.addGestureRecognizer(panGesture)
return imageView
}
func getDropZoneCard() -> DropZoneCard {
let dropZone = DropZoneCard()
dropZone.isUserInteractionEnabled = true
dropZone.backgroundColor = .yellow
return dropZone
}
func addNewCard() {
let imageView = getNewCard()
view.addSubview(imageView)
}
#objc func handleGesture(_ recognizer: UIPanGestureRecognizer) {
let translation = recognizer.translation(in: self.view)
if let view = recognizer.view {
view.center = CGPoint(x:view.center.x + translation.x,
y:view.center.y + translation.y)
if recognizer.state == .ended {
let point = view.center
for dropZone in arrayDropZones {
if dropZone.frame.contains(point) {
dropZone.append(card: view)
addNewCard()
return
}
}
view.frame = initialFrame
}
}
recognizer.setTranslation(.zero, in: view)
}
}
class DropZoneCard: UIImageView {
private(set) var arrayCards = [UIView]()
func append(card: UIView) {
arrayCards.append(card)
card.isUserInteractionEnabled = false
card.frame = frame
}
}

How to show image center position inside focus view while scrolling and crop image

I'm trying to zoom image and crop image along the focusView line.
But image's position isn't center while scrolling.
Also cropImage() doesn't crop properly.
I've already tried below code.
setup() image shows center but after scrolling, image position isn't center.
Also cropImage()
class CropImageView: UIView {
#IBOutlet weak var focusView: UIImage! // crop frame
#IBOutlet weak var scrollView: UIScrollView!
var previewImage: UIImage?
var previewImageView: UIImageView!
var zoomScaleView: UIView!
func setup() {
scrollView.delegate = self
zoomScaleView = UIView(frame: .zero)
scrollView.addSubview(zoomScaleView)
previewImageView = UIImageView(frame: .zero)
previewImageView.image = previewImage
zoomScaleView.addSubview(previewImageView)
scrollView.minimumZoomScale = 1
scrollView.maximumZoomScale = 3
previewImageView.frame = CGRect(x: 0, y: 0,width: focusView.frame.width, height: focusView.frame.width)
scrollView.contentSize = CGSize(width: focusView.frame.width, height: focusView.frame.width)
scrollView.contentOffset = CGPoint(x: 0, y: 0)
previewImageView.contentMode = .scaleAspectFit
previewImageView.backgroundColor = .red
}
func scrollViewDidZoom(_ scrollView: UIScrollView) {
let scrollZoomScale = scrollView.zoomScale
if scrollZoomScale == scrollView.minimumZoomScale {
scrollView.contentInset = UIEdgeInsets.zero
return
}
if previewImageView.contentClippingRect.width <= previewImageView.contentClippingRect.height {
let difference = (previewImageView.frame.width * scrollZoomScale - previewImageView.contentClippingRect.width * scrollZoomScale)
scrollView.contentSize = CGSize(width: previewImageView.frame.width * scrollZoomScale - difference,
height: previewImageView.frame.height * scrollZoomScale)
scrollView.contentInset = UIEdgeInsets.init(
top: 0,
left: -difference/2,
bottom: (self.frame.height - focusView.frame.maxY),
right: difference/2)
} else {
let difference = (previewImageView.frame.height * scrollZoomScale - previewImageView.contentClippingRect.height * scrollZoomScale)
scrollView.contentSize = CGSize(width: previewImageView.frame.width * scrollZoomScale,
height: previewImageView.frame.height * scrollZoomScale - difference)
scrollView.contentInset = UIEdgeInsets.init(
top: -difference/2,
left: 0 ,
bottom: (self.frame.height - focusView.frame.maxY) + difference/2,
right: 0)
}
}
func cropImage() -> UIImage {
return previewImageView.image?.cgImage?.cropping(to: focusView.frame)
}
}
extension UIImageView {
var contentClippingRect: CGRect {
guard let image = image else { return bounds }
guard contentMode == .scaleAspectFit else { return bounds }
guard image.size.width > 0 && image.size.height > 0 else { return bounds }
let scale: CGFloat
if image.size.width > image.size.height {
scale = bounds.width / image.size.width
} else {
scale = bounds.height / image.size.height
}
let size = CGSize(width: image.size.width * scale, height: image.size.height * scale)
let x = (bounds.width - size.width) / 2.0
let y = (bounds.height - size.height) / 2.0
return CGRect(x: x, y: y, width: size.width, height: size.height)
}
}
[Setup]
[After zooming]
I'm struggling with this problem for long time.
Is there any solution?
Thank you.

UITableView cell height for iOS 7.1.2

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.

swift change uiviewcontroller's view

I create a new UIViewController, I don't use storyboard, this is my code. I want to change my view frame, this not work for me, I had try to add on viewWillAppear, it's still not work, I know I can add a new UIView to do it. Can I change my viewcontroller's view? Thanks your help.
import UIKit
class NewDetailViewController: UIViewController {
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
}
override func viewDidLoad() {
super.viewDidLoad()
let statusBarHeight: CGFloat = UIApplication.sharedApplication().statusBarFrame.height
let navBarHeight = self.navigationController?.navigationBar.frame.size.height
println("statusBarHeight: \(statusBarHeight) navBarHeight: \(navBarHeight)")
// view
var x:CGFloat = self.view.bounds.origin.x
var y:CGFloat = self.view.bounds.origin.y + statusBarHeight + CGFloat(navBarHeight!)
var width:CGFloat = self.view.bounds.width
var height:CGFloat = self.view.bounds.height - statusBarHeight - CGFloat(navBarHeight!)
var frame:CGRect = CGRect(x: x, y: y, width: width, height: 100)
println("x: \(x) y: \(y) width: \(width) height: \(height)")
self.view.frame = frame
self.view.backgroundColor = UIColor.redColor()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
I found I add on viewDidLayoutSubviews, it work for me.
override func viewDidLayoutSubviews() {
let statusBarHeight: CGFloat = UIApplication.sharedApplication().statusBarFrame.height
let navBarHeight = self.navigationController?.navigationBar.frame.size.height
println("statusBarHeight: \(statusBarHeight) navBarHeight: \(navBarHeight)")
// view
var x:CGFloat = self.view.bounds.origin.x
var y:CGFloat = self.view.bounds.origin.y + statusBarHeight + CGFloat(navBarHeight!)
var width:CGFloat = self.view.bounds.width
var height:CGFloat = self.view.bounds.height - statusBarHeight - CGFloat(navBarHeight!)
var frame:CGRect = CGRect(x: x, y: y, width: width, height: height)
println("x: \(x) y: \(y) width: \(width) height: \(height)")
self.view.frame = frame
self.view.backgroundColor = UIColor.redColor()
}

Resources