Everything in the paged scroll view is to the right - swift

I have looked at similar problems on stack overflow and google, however my problem is not solved. I designed an app which uses a page scroll view in the Main.storyboard, which scrolls horizontally between other storyboards[pages]. I used the video Snapchat-Like Menu by Jared Davidson to help me.
Before, It was in the center, as I would expect it to. I think it changed when I updated to Xcode 8(But am not certain). However, everything is to the right. I think it changed when I updated to Xcode 8(But am not certain) I have tried updating the frames but it has been unsuccessful. Attached are some images to help show you the problem as well as my ViewController code.
What could I do?
Thank you in advance.
//
// ViewController.swift
//
//
// Created by Lucas Farleigh on 13/08/2016.
//
import UIKit
class ViewController: UIViewController {
#IBOutlet var MyScrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
//Declaring the child views
let VM : TitlePageViewController = TitlePageViewController(nibName: "TitlePageViewController", bundle: nil)
let V1 : View1 = View1(nibName: "View1", bundle: nil)
let V2 : View2 = View2(nibName: "View2", bundle: nil)
let V3 : View3 = View3(nibName: "View3", bundle: nil)
let V4 : View4 = View4(nibName: "View4", bundle: nil)
let V5 : View5 = View5(nibName: "View5", bundle: nil)
let V6 : View6 = View6(nibName: "View6", bundle: nil)
let V7 : View7 = View7(nibName: "View7", bundle: nil)
let V8 : View8 = View8(nibName: "View8", bundle: nil)
let V9 : View9 = View9(nibName: "View9", bundle: nil)
let V10 : View10 = View10(nibName: "View10", bundle: nil)
let V11 : View11 = View11(nibName: "View11", bundle: nil)
let V12 : View12 = View12(nibName: "View12", bundle: nil)
let V13 : View13 = View13(nibName: "View13", bundle: nil)
let V14 : View14 = View14(nibName: "View14", bundle: nil)
let V15 : View15 = View15(nibName: "View15", bundle: nil)
let V16 : View16 = View16(nibName: "View16", bundle: nil)
let V17 : View17 = View17(nibName: "View17", bundle: nil)
let V18 : View18 = View18(nibName: "View18", bundle: nil)
let V19 : View19 = View19(nibName: "View19", bundle: nil)
let V20 : View20 = View20(nibName: "View20", bundle: nil)
let V21 : View21 = View21(nibName: "View21", bundle: nil)
let V22 : View22 = View22(nibName: "View22", bundle: nil)
let V23 : View23 = View23(nibName: "View23", bundle: nil)
let V24 : View24 = View24(nibName: "View24", bundle: nil)
let V25 : View25 = View25(nibName: "View25", bundle: nil)
let V26 : View26 = View26(nibName: "View26", bundle: nil)
let V27 : View27 = View27(nibName: "View27", bundle: nil)
let V28 : View28 = View28(nibName: "View28", bundle: nil)
let V29 : View29 = View29(nibName: "View29", bundle: nil)
let V30 : View30 = View30(nibName: "View30", bundle: nil)
let V31 : View31 = View31(nibName: "View31", bundle: nil)
let V32 : View32 = View32(nibName: "View32", bundle: nil)
let V33 : View33 = View33(nibName: "View33", bundle: nil)
let V34 : View34 = View34(nibName: "View34", bundle: nil)
let V35 : View35 = View35(nibName: "View35", bundle: nil)
let V36 : View36 = View36(nibName: "View36", bundle: nil)
let V37 : View37 = View37(nibName: "View37", bundle: nil)
let V38 : View38 = View38(nibName: "View38", bundle: nil)
let V39 : View39 = View39(nibName: "View39", bundle: nil)
let V40 : View40 = View40(nibName: "View40", bundle: nil)
let V41 : View39 = View39(nibName: "View41", bundle: nil)
let V42 : View40 = View40(nibName: "View42", bundle: nil)
//Decraling that the child views are child views
self.addChildViewController(VM)
self.addChildViewController(V1)
self.addChildViewController(V2)
self.addChildViewController(V3)
self.addChildViewController(V4)
self.addChildViewController(V5)
self.addChildViewController(V6)
self.addChildViewController(V7)
self.addChildViewController(V8)
self.addChildViewController(V9)
self.addChildViewController(V10)
self.addChildViewController(V11)
self.addChildViewController(V12)
self.addChildViewController(V13)
self.addChildViewController(V14)
self.addChildViewController(V15)
self.addChildViewController(V16)
self.addChildViewController(V17)
self.addChildViewController(V18)
self.addChildViewController(V19)
self.addChildViewController(V20)
self.addChildViewController(V21)
self.addChildViewController(V22)
self.addChildViewController(V23)
self.addChildViewController(V24)
self.addChildViewController(V25)
self.addChildViewController(V26)
self.addChildViewController(V27)
self.addChildViewController(V28)
self.addChildViewController(V29)
self.addChildViewController(V30)
self.addChildViewController(V31)
self.addChildViewController(V32)
self.addChildViewController(V33)
self.addChildViewController(V34)
self.addChildViewController(V35)
self.addChildViewController(V36)
self.addChildViewController(V37)
self.addChildViewController(V38)
self.addChildViewController(V39)
self.addChildViewController(V40)
self.addChildViewController(V41)
self.addChildViewController(V42)
//Adding the view into the scrollview (MyScrollView)
/**/ self.MyScrollView.addSubview(VM.view)
self.MyScrollView.addSubview(V1.view)
self.MyScrollView.addSubview(V2.view)
self.MyScrollView.addSubview(V3.view)
self.MyScrollView.addSubview(V4.view)
self.MyScrollView.addSubview(V5.view)
self.MyScrollView.addSubview(V6.view)
self.MyScrollView.addSubview(V7.view)
self.MyScrollView.addSubview(V8.view)
self.MyScrollView.addSubview(V9.view)
self.MyScrollView.addSubview(V10.view)
self.MyScrollView.addSubview(V11.view)
self.MyScrollView.addSubview(V12.view)
self.MyScrollView.addSubview(V13.view)
self.MyScrollView.addSubview(V14.view)
self.MyScrollView.addSubview(V15.view)
self.MyScrollView.addSubview(V16.view)
self.MyScrollView.addSubview(V17.view)
self.MyScrollView.addSubview(V18.view)
self.MyScrollView.addSubview(V19.view)
self.MyScrollView.addSubview(V20.view)
self.MyScrollView.addSubview(V21.view)
self.MyScrollView.addSubview(V22.view)
self.MyScrollView.addSubview(V23.view)
self.MyScrollView.addSubview(V24.view)
self.MyScrollView.addSubview(V25.view)
self.MyScrollView.addSubview(V26.view)
self.MyScrollView.addSubview(V27.view)
self.MyScrollView.addSubview(V28.view)
self.MyScrollView.addSubview(V29.view)
self.MyScrollView.addSubview(V30.view)
self.MyScrollView.addSubview(V31.view)
self.MyScrollView.addSubview(V32.view)
self.MyScrollView.addSubview(V33.view)
self.MyScrollView.addSubview(V34.view)
self.MyScrollView.addSubview(V35.view)
self.MyScrollView.addSubview(V36.view)
self.MyScrollView.addSubview(V37.view)
self.MyScrollView.addSubview(V38.view)
self.MyScrollView.addSubview(V39.view)
self.MyScrollView.addSubview(V40.view)
self.MyScrollView.addSubview(V41.view)
self.MyScrollView.addSubview(V42.view)
//Confirming that it did move to the view controller
/**/ VM.didMove(toParentViewController: self)
V1.didMove(toParentViewController: self)
V2.didMove(toParentViewController: self)
V3.didMove(toParentViewController: self)
V4.didMove(toParentViewController: self)
V5.didMove(toParentViewController: self)
V6.didMove(toParentViewController: self)
V7.didMove(toParentViewController: self)
V8.didMove(toParentViewController: self)
V9.didMove(toParentViewController: self)
V10.didMove(toParentViewController: self)
V11.didMove(toParentViewController: self)
V12.didMove(toParentViewController: self)
V13.didMove(toParentViewController: self)
V14.didMove(toParentViewController: self)
V15.didMove(toParentViewController: self)
V16.didMove(toParentViewController: self)
V17.didMove(toParentViewController: self)
V18.didMove(toParentViewController: self)
V19.didMove(toParentViewController: self)
V20.didMove(toParentViewController: self)
V21.didMove(toParentViewController: self)
V22.didMove(toParentViewController: self)
V23.didMove(toParentViewController: self)
V24.didMove(toParentViewController: self)
V25.didMove(toParentViewController: self)
V26.didMove(toParentViewController: self)
V27.didMove(toParentViewController: self)
V28.didMove(toParentViewController: self)
V29.didMove(toParentViewController: self)
V30.didMove(toParentViewController: self)
V31.didMove(toParentViewController: self)
V32.didMove(toParentViewController: self)
V33.didMove(toParentViewController: self)
V34.didMove(toParentViewController: self)
V35.didMove(toParentViewController: self)
V36.didMove(toParentViewController: self)
V37.didMove(toParentViewController: self)
V38.didMove(toParentViewController: self)
V39.didMove(toParentViewController: self)
V40.didMove(toParentViewController: self)
V41.didMove(toParentViewController: self)
V42.didMove(toParentViewController: self)
//SIZES AND POSITIONS
//Declaring how big the scroll view is
self.MyScrollView.contentSize = CGSize(width: self.view.frame.width * 43, height: self.view.frame.height)
//Setting the frame of the Views
var V1Frame : CGRect = V1.view.frame
var V2Frame : CGRect = V2.view.frame
var V3Frame : CGRect = V3.view.frame
var V4Frame : CGRect = V4.view.frame
var V5Frame : CGRect = V5.view.frame
var V6Frame : CGRect = V6.view.frame
var V7Frame : CGRect = V7.view.frame
var V8Frame : CGRect = V8.view.frame
var V9Frame : CGRect = V9.view.frame
var V10Frame : CGRect = V10.view.frame
var V11Frame : CGRect = V11.view.frame
var V12Frame : CGRect = V12.view.frame
var V13Frame : CGRect = V13.view.frame
var V14Frame : CGRect = V14.view.frame
var V15Frame : CGRect = V15.view.frame
var V16Frame : CGRect = V16.view.frame
var V17Frame : CGRect = V17.view.frame
var V18Frame : CGRect = V18.view.frame
var V19Frame : CGRect = V19.view.frame
var V20Frame : CGRect = V20.view.frame
var V21Frame : CGRect = V21.view.frame
var V22Frame : CGRect = V22.view.frame
var V23Frame : CGRect = V23.view.frame
var V24Frame : CGRect = V24.view.frame
var V25Frame : CGRect = V25.view.frame
var V26Frame : CGRect = V26.view.frame
var V27Frame : CGRect = V27.view.frame
var V28Frame : CGRect = V28.view.frame
var V29Frame : CGRect = V29.view.frame
var V30Frame : CGRect = V30.view.frame
var V31Frame : CGRect = V31.view.frame
var V32Frame : CGRect = V32.view.frame
var V33Frame : CGRect = V33.view.frame
var V34Frame : CGRect = V34.view.frame
var V35Frame : CGRect = V35.view.frame
var V36Frame : CGRect = V36.view.frame
var V37Frame : CGRect = V37.view.frame
var V38Frame : CGRect = V38.view.frame
var V39Frame : CGRect = V39.view.frame
var V40Frame : CGRect = V40.view.frame
var V41Frame : CGRect = V41.view.frame
var V42Frame : CGRect = V42.view.frame
//Making it off the screen to the right
V1Frame.origin.x = self.view.frame.width
V2Frame.origin.x = 2 * self.view.frame.width
V3Frame.origin.x = 3 * self.view.frame.width
V4Frame.origin.x = 4 * self.view.frame.width
V5Frame.origin.x = 5 * self.view.frame.width
V6Frame.origin.x = 6 * self.view.frame.width
V7Frame.origin.x = 7 * self.view.frame.width
V8Frame.origin.x = 8 * self.view.frame.width
V9Frame.origin.x = 9 * self.view.frame.width
V10Frame.origin.x = 10 * self.view.frame.width
V11Frame.origin.x = 11 * self.view.frame.width
V12Frame.origin.x = 12 * self.view.frame.width
V13Frame.origin.x = 13 * self.view.frame.width
V14Frame.origin.x = 14 * self.view.frame.width
V15Frame.origin.x = 15 * self.view.frame.width
V16Frame.origin.x = 16 * self.view.frame.width
V17Frame.origin.x = 17 * self.view.frame.width
V18Frame.origin.x = 18 * self.view.frame.width
V19Frame.origin.x = 19 * self.view.frame.width
V20Frame.origin.x = 20 * self.view.frame.width
V21Frame.origin.x = 21 * self.view.frame.width
V22Frame.origin.x = 22 * self.view.frame.width
V23Frame.origin.x = 23 * self.view.frame.width
V24Frame.origin.x = 24 * self.view.frame.width
V25Frame.origin.x = 25 * self.view.frame.width
V26Frame.origin.x = 26 * self.view.frame.width
V27Frame.origin.x = 27 * self.view.frame.width
V28Frame.origin.x = 28 * self.view.frame.width
V29Frame.origin.x = 29 * self.view.frame.width
V30Frame.origin.x = 30 * self.view.frame.width
V31Frame.origin.x = 31 * self.view.frame.width
V32Frame.origin.x = 32 * self.view.frame.width
V33Frame.origin.x = 33 * self.view.frame.width
V34Frame.origin.x = 34 * self.view.frame.width
V35Frame.origin.x = 35 * self.view.frame.width
V36Frame.origin.x = 36 * self.view.frame.width
V37Frame.origin.x = 37 * self.view.frame.width
V38Frame.origin.x = 38 * self.view.frame.width
V39Frame.origin.x = 39 * self.view.frame.width
V40Frame.origin.x = 40 * self.view.frame.width
V41Frame.origin.x = 41 * self.view.frame.width
V42Frame.origin.x = 42 * self.view.frame.width
//Adding it on to the view
V1.view.frame = V1Frame
V2.view.frame = V2Frame
V3.view.frame = V3Frame
V4.view.frame = V4Frame
V5.view.frame = V5Frame
V6.view.frame = V6Frame
V7.view.frame = V7Frame
V8.view.frame = V8Frame
V9.view.frame = V9Frame
V10.view.frame = V10Frame
V11.view.frame = V11Frame
V12.view.frame = V12Frame
V13.view.frame = V13Frame
V14.view.frame = V14Frame
V15.view.frame = V15Frame
V16.view.frame = V16Frame
V17.view.frame = V17Frame
V18.view.frame = V18Frame
V19.view.frame = V19Frame
V20.view.frame = V20Frame
V21.view.frame = V21Frame
V22.view.frame = V22Frame
V23.view.frame = V23Frame
V24.view.frame = V24Frame
V25.view.frame = V25Frame
V26.view.frame = V26Frame
V27.view.frame = V27Frame
V28.view.frame = V28Frame
V29.view.frame = V29Frame
V30.view.frame = V30Frame
V31.view.frame = V31Frame
V32.view.frame = V32Frame
V33.view.frame = V33Frame
V34.view.frame = V34Frame
V35.view.frame = V35Frame
V36.view.frame = V36Frame
V37.view.frame = V37Frame
V38.view.frame = V38Frame
V39.view.frame = V39Frame
V40.view.frame = V40Frame
V41.view.frame = V41Frame
V42.view.frame = V42Frame
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

This doesn't directly answer your question, but what you're doing is totally wrong and you need to abandon it. 42 view controllers with 42 screen-sized views, all of them in the scroll view at the same time, is just bad practice.
Switch to using UIPageViewController. It is a paged horizontal scroller, it does what you're trying to do, and it does it efficiently — only one view controller and one view exists at any one moment.

Related

CAEmitterLayer doesn't work in MacOS with Swift

I just simply create a storyboard program, and change the main ViewController code to the code below which works well in others' example. but it appears nothing on the view? seeking help.
What I did:
create an project with storyboard, and I got 'AppDelegate.swift' and 'ViewController.swift'
paste 'Ball_green.png' to my project and see it under the file tree of my project.
paste the code below in 'ViewController.swift'
import Cocoa
class ViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
self.view.wantsLayer = true
self.addLayer()
}
func addLayer(){
let rootLayer = self.view.layer
let snowEmitter = CAEmitterLayer()
snowEmitter.drawsAsynchronously = true
snowEmitter.name = "snowEmitter"
snowEmitter.zPosition = 10.0
snowEmitter.emitterPosition = CGPoint(x: 0, y: 0)
snowEmitter.renderMode = CAEmitterLayerRenderMode.backToFront
snowEmitter.emitterShape = CAEmitterLayerEmitterShape.circle
snowEmitter.emitterZPosition = -43.00
snowEmitter.emitterSize = CGSize(width: 160, height: 160)
snowEmitter.velocity = 20.57
snowEmitter.emitterMode = CAEmitterLayerEmitterMode.points
snowEmitter.birthRate = 10
let snowFlakesCell2 = CAEmitterCell()
snowFlakesCell2.emissionRange = .pi
snowFlakesCell2.lifetime = 10.0
snowFlakesCell2.birthRate = 4.0
snowFlakesCell2.velocity = 2.0
snowFlakesCell2.velocityRange = 100.0
snowFlakesCell2.yAcceleration = 300.0
snowFlakesCell2.contents = NSImage(named: "Ball_green.png")!.cgImage
snowFlakesCell2.magnificationFilter = convertFromCALayerContentsFilter(CALayerContentsFilter.nearest)
snowFlakesCell2.scale = 0.72
snowFlakesCell2.scaleRange = 0.14
snowFlakesCell2.spin = 0.38
snowFlakesCell2.spinRange = 0
snowEmitter.emitterCells = [snowFlakesCell2]
rootLayer!.addSublayer(snowEmitter)
}
}
fileprivate func convertFromCALayerContentsFilter(_ input: CALayerContentsFilter) -> String {
return input.rawValue
}
extension NSImage {
var cgImage: CGImage {
get {
let imageData = self.tiffRepresentation
let source = CGImageSourceCreateWithData(imageData as! CFData, nil)
let maskRef = CGImageSourceCreateImageAtIndex(source!, 0, nil)
return maskRef!
}
}
}
I've solved the problem after adding the code.

UIImage rotates after being cropped

I have a class setup that allows users to add an image from their library, crop it and save it.
The code is set up so that if the retrieved image is portrait, a portrait shaped border appears to all them to align before cropping and if Landscape, a landscaped border appears.
If the image selected is a regular shaped image, all works well. However, if the image retrieved is portrait and not of a regular ratio (meaning closer to a square shape while not actually being square), the image rotates after being cropped. It seems as thought the system is treating it like a landscape image.
Here is an example of before and after crop. Even if I zoom in and make the image cover the entire screen, it rotates the image:
import Foundation
import UIKit
class SelectImageViewController: UIViewController, UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIScrollViewDelegate{
#IBOutlet weak var imageView: UIImageView!
#IBOutlet weak var imageConstraintTop: NSLayoutConstraint!
#IBOutlet weak var imageConstraintRight: NSLayoutConstraint!
#IBOutlet weak var imageConstraintLeft: NSLayoutConstraint!
#IBOutlet weak var imageConstraintBottom: NSLayoutConstraint!
var lastZoomScale: CGFloat = -1
var imageName: String = ""
var userPhotoUUID = UUID().uuidString
let userDefault = UserDefaults.standard
var userDatabase: UserDatabase = UserDatabase()
let picker = UIImagePickerController()
#IBOutlet var scrollView: UIScrollView!{
didSet{
scrollView.delegate = self
scrollView.minimumZoomScale = 1.0
scrollView.maximumZoomScale = 5.0
}
}
#IBOutlet weak var ratioSelector: UISegmentedControl!
#IBOutlet var cropAreaViewL: CropAreaViewL!
var cropAreaL:CGRect{
get{
let factor = imageView.image!.size.width/view.frame.width
let scale = 1/scrollView.zoomScale
let imageFrame = imageView.imageFrame()
let x = (scrollView.contentOffset.x + cropAreaViewL.frame.origin.x - imageFrame.origin.x) * scale * factor
let y = (scrollView.contentOffset.y + cropAreaViewL.frame.origin.y - imageFrame.origin.y) * scale * factor
let width = cropAreaViewL.frame.size.width * scale * factor
let height = cropAreaViewL.frame.size.height * scale * factor
return CGRect(x: x, y: y, width: width, height: height)
}
}
#IBOutlet var cropAreaViewP: CropAreaViewP!
var cropAreaP:CGRect{
get{
let factor = imageView.image!.size.height/view.frame.height
let scale = 1/scrollView.zoomScale
let imageFrame = imageView.imageFrame()
let x = (scrollView.contentOffset.x + cropAreaViewP.frame.origin.x - imageFrame.origin.x) * scale * factor
let y = (scrollView.contentOffset.y + cropAreaViewP.frame.origin.y - imageFrame.origin.y) * scale * factor
let width = cropAreaViewP.frame.size.width * scale * factor
let height = cropAreaViewP.frame.size.height * scale * factor
return CGRect(x: x, y: y, width: width, height: height)
}
}
fileprivate var speciePhotos: Array<SpeciePhotoModel> = [SpeciePhotoModel]()
func randomNumber(range: ClosedRange<Int> = 30000...99998) -> Int {
let min = range.lowerBound
let max = range.upperBound
return Int(arc4random_uniform(UInt32(1 + max - min))) + min
}
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "+", style: .plain, target: self, action: #selector(SelectImageViewController.add(_:)))
let id = randomNumber()
userDefault.set(id, forKey: "photoID")
self.cropAreaViewP.isHidden = true
self.cropAreaViewL.isHidden = true
self.cropAreaViewL.layer.borderColor = (UIColor.red).cgColor
self.cropAreaViewL.layer.borderWidth = 1.0
self.cropAreaViewP.layer.borderColor = (UIColor.red).cgColor
self.cropAreaViewP.layer.borderWidth = 1.0
self.add.layer.cornerRadius = 6.0
self.ratioSelector.layer.cornerRadius = 6.0
self.tabBarController?.tabBar.isHidden = true
self.add.isHidden = true
self.ratioSelector.isHidden = true
updateZoom()
}
func updateConstraints() {
if let image = imageView.image {
let imageWidth = image.size.width
let imageHeight = image.size.height
let viewWidth = scrollView.bounds.size.width
let viewHeight = scrollView.bounds.size.height
// center image if it is smaller than the scroll view
var hPadding = (viewWidth - scrollView.zoomScale * imageWidth) / 2
if hPadding < 0 { hPadding = 0 }
var vPadding = (viewHeight - scrollView.zoomScale * imageHeight) / 2
if vPadding < 0 { vPadding = 0 }
imageConstraintLeft.constant = hPadding
imageConstraintRight.constant = hPadding
imageConstraintTop.constant = vPadding
imageConstraintBottom.constant = vPadding
view.layoutIfNeeded()
}
}
fileprivate func updateZoom() {
if let image = imageView.image {
var minZoom = min(scrollView.bounds.size.width / image.size.width,
scrollView.bounds.size.height / image.size.height)
if minZoom > 1 { minZoom = 1 }
scrollView.minimumZoomScale = 0.3 * minZoom
// Force scrollViewDidZoom fire if zoom did not change
if minZoom == lastZoomScale { minZoom += 0.000001 }
scrollView.zoomScale = minZoom
lastZoomScale = minZoom
}
}
#IBAction func ratioSelector(_ sender: AnyObject) {
switch ratioSelector.selectedSegmentIndex
{
case 0:// Landscape
self.cropAreaViewP.isHidden = true
self.cropAreaViewL.isHidden = false
case 1: // Portrait
self.cropAreaViewL.isHidden = true
self.cropAreaViewP.isHidden = false
default:
break;
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
#IBOutlet weak var add : UIButton!
#IBAction func add(_ sender: UIButton) {
imageView.image = nil
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .photoLibrary
picker.allowsEditing = false
self.present(picker, animated: true, completion: nil)
self.ratioSelector.isHidden = false
self.add.isHidden = false
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Crop", style: .plain, target: self, action: #selector(SelectImageViewController.crop(_:)))
}
#IBAction func change(_ sender: UIButton) {
imageView.image = nil
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .photoLibrary
picker.allowsEditing = false
self.present(picker, animated: true, completion: nil)
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Crop", style: .plain, target: self, action: #selector(SelectImageViewController.crop(_:)))
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
if chosenImage.size.height > chosenImage.size.width
{
self.cropAreaViewL.isHidden = true
self.cropAreaViewP.isHidden = false
self.ratioSelector.selectedSegmentIndex = 1
imageView.image = chosenImage
}
else
{
self.cropAreaViewP.isHidden = true
self.cropAreaViewL.isHidden = false
self.ratioSelector.selectedSegmentIndex = 0
imageView.image = chosenImage
}
self.dismiss(animated: true, completion: nil)
}
#IBAction func crop(_ sender: UIButton) {
if cropAreaViewP.isHidden == true {
self.cropAreaViewL.layer.borderColor = (UIColor.clear).cgColor
let croppedCGImage = imageView.image?.cgImage?.cropping(to: cropAreaL)
let croppedImage = UIImage(cgImage: croppedCGImage!)
imageView.image = croppedImage
scrollView.zoomScale = 1
} else {
self.cropAreaViewP.layer.borderColor = (UIColor.clear).cgColor
let croppedCGImage = imageView.image?.cgImage?.cropping(to: cropAreaP)
let croppedImage = UIImage(cgImage: croppedCGImage!)
imageView.image = croppedImage
scrollView.zoomScale = 1
}
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Next", style: .plain, target: self, action: #selector(SelectImageViewController.saveButtonAction(_:)))
}
}
extension UIImageView{
func imageFrame()->CGRect{
let imageViewSize = self.frame.size
guard let imageSize = self.image?.size else{return CGRect.zero}
let imageRatio = imageSize.width / imageSize.height
let imageViewRatio = imageViewSize.width / imageViewSize.height
if imageRatio < imageViewRatio { // Portrait
let scaleFactor = imageViewSize.height / imageSize.height
let width = imageSize.width * scaleFactor
let topLeftX = (imageViewSize.width - width) * 0.5
return CGRect(x: topLeftX, y: 0, width: width, height: imageViewSize.height)
}else{ // Landscape
let scaleFactor = imageViewSize.width / imageSize.width
let height = imageSize.height * scaleFactor
let topLeftY = (imageViewSize.height - height) * 0.5
return CGRect(x: 0, y: topLeftY, width: imageViewSize.width, height: height)
}
}
}
class CropAreaViewL: UIView {
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
return false
}
}
class CropAreaViewP: UIView {
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
return false
}
}
Any help would be huge.
You can use max method to make sure you don't get a value lower than zero:
let vPadding = max((viewHeight - scrollView.zoomScale * imageHeight) / 2, 0)
If you need to make your image squared you can do it as follow:
extension UIImage {
var isPortrait: Bool { return size.height > size.width }
var isLandscape: Bool { return size.width > size.height }
var breadth: CGFloat { return min(size.width, size.height) }
var breadthSize: CGSize { return CGSize(width: breadth, height: breadth) }
var squared: UIImage? {
guard let cgImage = cgImage?.cropping(to:
CGRect(origin: CGPoint(x: isLandscape ? floor((size.width-size.height)/2) : 0, y: isPortrait ? floor((size.height-size.width)/2) : 0),
size: breadthSize)) else { return nil }
return UIImage(cgImage: cgImage)
}
}
To fix the orientation issue you need to redraw your image you can use the flatten property from this answer.
Playground:
let profilePicture = UIImage(data: try! Data(contentsOf: URL(string:"https://i.stack.imgur.com/Xs4RX.jpg")!))!
if let squared = profilePicture.squared {
squared
}

Cannot change the size of a popover

I've been trying to create my own code, as well as copy and paste examples, but my popover seems to fill the entire screen. Generally my code looks like below, but like I said, I've tried at least two other examples (some with CGSizeMake(), sourceRect, sourceView), and the results are always the same.
Class TableView: UITableViewController, UIPopoverPresentationControllerDelegate {
let popover = UIViewController()
func someButton() {
popover.modalPresentationStyle = .Popover
popover.preferredContentSize = CGSize(1, 1)
let menu = popover.popoverPresentationController
menu!.delegate = self
menu!.barButtonItem = someOtherCorrectButton
self.presentViewController(popover, animated: true, completion: nil)
}
}
What's the problem?
Try below code
popover.modalPresentationStyle = .Popover
var popupHeight:CGFloat = 542
let popupWidth = 400 as CGFloat
popover.preferredContentSize = CGSizeMake(400,popupHeight)
let window = UIApplication.sharedApplication().delegate!.window!!
let screenSize: CGRect = window.bounds
var XCordinate = (screenSize.width - popupWidth) / 2
var YCordinate = (screenSize.height / 2) + 20
let menu = popover.popoverPresentationController
menu!.delegate = self
menu!.sourceView = window
menu!.sourceRect = CGRect( x: XCordinate, y: YCordinate, width: 1, height: 1)
self.presentViewController(popover, animated: true, completion: nil)
May be it works for you.

Add UIView in Main View As UItabBarController in swift

I am adding Tabbarcontroller in Main View, but i didn't set its background colour... I have already try tabBar.barTintColor but it's not working..
So i am trying to Add View as TabbarController (Which work like UITabbar) but i am not succeeded on it... Can any one please help me out.Here is the code
var tabBar = UITabBarController()
var frameOfView : CGRect = UIScreen.mainScreen().bounds
tabBar.view.frame = CGRectMake(frameOfView.origin.x, frameOfView.size.height, frameOfView.size.width, frameOfView.size.height-100)
var viewTabBar = UIView(frame: CGRectMake(frameOfView.origin.x, 430, frameOfView.size.width, 100))
var BtnHomeTab = UIButton(frame: CGRectMake(0, 0, 30, 30))
var imageHomeTab = UIImage(named: "home_selected.png")
viewTabBar.addSubview(BtnHomeTab)
have to hide UITabbarcontroller ..:)
and then Add this code to Your appDelegate.
//MARK: CREATE BOTTOMBAR BUTTON
func craeteBottonBarButton()
{
var viewBottom : UIView = UIView(frame: CGRectMake(0,window!.frame.size.height - 50.0,window!.frame.size.width,60.0))
viewBottom.tag = TagWindow.Tag_Bottom.rawValue
window!.addSubview(viewBottom)
window!.bringSubviewToFront(viewBottom)
viewBottom.backgroundColor = GlobalConstants.GlobalConstants.ButtonConstant.BUTTONBACKGROUNDCOLOR
viewBottom.tag = TagWindow.Tag_Bottom.rawValue
var x : CGFloat = 0.0
for var i = 0 ; i < 5 ; i++
{
var btn = createTabBarButton(viewBottom,
framebtn: CGRectMake(x,0, 65.0, 50.0),
tag: (i + 1),
imgName: arrayImage.objectAtIndex(i) as NSString , btnName: arrayLblName.objectAtIndex(i) as NSString) as UIButton
viewBottom.addSubview(btn)
x += 64.5
}
}
Give all navigation controller to the button
func createTabBar() -> UITabBarController
{
var storyBoard = UIStoryboard(name: "Main", bundle: nil)
tabBar.delegate = self
var tabBarFont = UITabBar()
let HomeTab = storyBoard.instantiateViewControllerWithIdentifier("HomeVC") as HomeVC
let navigation_home = UINavigationController(rootViewController: HomeTab)
navigation_home.navigationBarHidden = true
let SearchTab = storyBoard.instantiateViewControllerWithIdentifier("SearchVC") as SearchVC
let navigation_search = UINavigationController(rootViewController: SearchTab)
navigation_search.navigationBarHidden = true
let SettingTab = storyBoard.instantiateViewControllerWithIdentifier("SettingVC") as SettingVC
let navigation_Setting = UINavigationController(rootViewController: SettingTab)
navigation_Setting.navigationBarHidden = true
let MySeatTab = storyBoard.instantiateViewControllerWithIdentifier("MySeatVC") as MySeatVC
let navigation_MySeat = UINavigationController(rootViewController: MySeatTab)
navigation_MySeat.navigationBarHidden = true

Delete ImageView From SubScrollView In Swift

I am having main ScrollView with pages Enable..Under that I have Subscrollviews and ImageView to display images..I want to Delete Selected image from main scrollview..I don't have any idea to how to get that selected imageView from scrollview and remove it...Here is my code...
func createScroll()
{
var arrayImage = NSUserDefaults.standardUserDefaults().objectForKey("ImageArray") as NSMutableArray
var index = NSUserDefaults.standardUserDefaults().objectForKey("Index") as Int
var scroll = UIScrollView(frame: CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height))
var page = UIPageControl(frame: CGRectMake(0, 0, scroll.frame.size.width, scroll.frame.size.height))
page.numberOfPages = arrayImage.count
page.currentPage = 0
page.autoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth
scroll.contentSize = CGSizeMake(scroll.frame.size.width * CGFloat(arrayImage.count - 1) , 0)
scroll.delegate = self
scroll.pagingEnabled = true
scroll.maximumZoomScale = 6
scroll.minimumZoomScale = 0.5
scroll.tag = Tags.Tag_MainScroll.rawValue
scroll.addSubview(page)
view.addSubview(scroll)
scroll.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
scroll.contentOffset = CGPointMake(scroll.frame.size.width * CGFloat(index - 1) , 0)
}
//MARK: Display Selected Image
func displayImage()
{
var mainScroll : UIScrollView = view.viewWithTag(Tags.Tag_MainScroll.rawValue) as UIScrollView
var arrayImage = NSUserDefaults.standardUserDefaults().objectForKey("ImageArray") as NSMutableArray
var xPosition : CGFloat = 0
for i in 1...22
{
var subScroll = UIScrollView(frame: CGRectMake(xPosition, 0, view.frame.size.width, view.frame.size.height))
subScroll.delegate = self
subScroll.maximumZoomScale = 6
subScroll.minimumZoomScale = 0.5
subScroll.tag = Tags.Tag_SubScroll.rawValue + i
mainScroll.addSubview(subScroll)
var imgToDisplay : UIImage = UIImage(named: "\(arrayImage[i])")! as UIImage
var imgView : UIImageView = UIImageView(frame: CGRectMake((subScroll.frame.size.width - 100 ) / 2,(subScroll.frame.size.height - 100) / 2, 100, 100))
subScroll.contentSize = CGSizeMake(imgView.frame.size.width, imgView.frame.size.height)
imgView.image = imgToDisplay
imgView.tag = Tags.Tag_imgView.rawValue
subScroll.addSubview(imgView)
xPosition += subScroll.frame.size.width
}
}
Here Is my Delete Function...
func Delete()
{
var imgView : UIImageView? = view.viewWithTag(Tags.Tag_imgView.rawValue) as? UIImageView
var scroll : UIScrollView = view.viewWithTag(Tags.Tag_MainScroll.rawValue) as UIScrollView
for img in scroll.subviews
{
imgView = img as? UIImageView
imgView?.removeFromSuperview()
}
}`
have u tryed this one..
let subViews = self.scrollView.subviews
for subview in subViews{
subview.removeFromSuperview()
}
HERE is the reference
as per my code this Delete Function is working for me
func Delete()
{
var imgView : UIImageView? = view.viewWithTag(Tags.Tag_imgView.rawValue) as? UIImageView
var scroll : UIScrollView = view.viewWithTag(Tags.Tag_MainScroll.rawValue) as UIScrollView
var number : Int = Int(scroll.contentOffset.x / scroll.frame.size.width)
var page : UIPageControl = view.viewWithTag(Tags.Tag_page.rawValue) as UIPageControl
page.currentPage = number
println("\(page.currentPage)")
if var array : NSArray = NSUserDefaults.standardUserDefaults().objectForKey("ImageArray") as? NSArray
{
arrayImage = array.mutableCopy() as NSMutableArray
}
page.removeFromSuperview()
number++
arrayImage.removeObjectAtIndex(number)
NSUserDefaults.standardUserDefaults() .setObject(arrayImage, forKey: "ImageArray")
println("\(arrayImage[number])")
println("\(arrayImage)")
NSUserDefaults.standardUserDefaults().synchronize()
displayImage()
}