Philip J
Philip J

Reputation: 76

Can't see my button when I run the program on my iPhone

Why can't I see my button when I run my program but in Xcode 8 I can see it. It's over a view and looks like the button you press to take a photo with in snapchat.

I'm new to Xcode and swift so if there is anything I need to know with Xcode or the storyboard that can help me with these problems please tell me. enter image description here

The code for the view:

 import UIKit
 import AVFoundation
 import QuartzCore

 class View1: UIViewController , AVCaptureVideoDataOutputSampleBufferDelegate{

let captureSession          = AVCaptureSession()

var previewLayer:               CALayer!

var captureDevice:              AVCaptureDevice!


@IBOutlet weak var cameraView:  UIView!

override func viewDidLoad() {
    super.viewDidLoad()


}


/*



 This is a function to prepair the camera

 and check that there is a camera.



 If there isn't a camera on the device

 then get you will get a error.



 */

func prepareCamera()

{



    captureSession.sessionPreset     = AVCaptureSessionPreset1920x1080





    if let availableDevices                  = AVCaptureDeviceDiscoverySession(deviceTypes: [.builtInWideAngleCamera],
                                                                                                                           mediaType: AVMediaTypeVideo,
                                                                                                                           position: .back).devices

    {



        captureDevice = availableDevices.first

        beginSession()



    }





}



func beginSession()

{



    do

    {



        let captureDeviceInput = try AVCaptureDeviceInput(device: captureDevice)

        captureSession.addInput(captureDeviceInput)



    }

    catch

    {



        print(error.localizedDescription)

        /*



         Figure out what to do here

 */

    }



    if let previewLayer                          = AVCaptureVideoPreviewLayer(session: captureSession)

    {



        self.previewLayer                        = previewLayer

        self.view.layer.addSublayer(
        self.previewLayer)

        self.previewLayer.frame                  = self.view.layer.frame

        self.previewLayer.frame.size             = self.view.layer.frame.size

        captureSession.startRunning()



        let dataOutput                           = AVCaptureVideoDataOutput()

        dataOutput.videoSettings                 = [(kCVPixelBufferPixelFormatTypeKey as NSString
                                                                        ): NSNumber(value: kCVPixelFormatType_32BGRA)]


        dataOutput.alwaysDiscardsLateVideoFrames = true


        if captureSession.canAddOutput(dataOutput)

        {

            captureSession.addOutput(dataOutput)


        }

        captureSession.commitConfiguration()

        let queue = DispatchQueue(label: "com.PhotoAllergy.captureQueue")
        dataOutput.setSampleBufferDelegate(self, queue: queue)



    }









}



//func captureOutput(_ captureOutput: AVCaptureOutput!, didDrop sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) {
//yeeye
//}

override func viewWillAppear(_ animated: Bool) {

    super.viewWillAppear(animated)

    prepareCamera()

}








override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}


 }

Upvotes: 0

Views: 502

Answers (2)

sarosh mirza
sarosh mirza

Reputation: 702

do the following

select your button in the storyboard view controller and follow the steps in the screen shot press the button add constraints run the app

enter image description here

Please make the viewController as the initial view controller as specified in the image below

enter image description here

Upvotes: 1

Faisal
Faisal

Reputation: 162

Add constraints. Follow these images

enter image description here enter image description here

Good luck, you can comment here if you have any questions about this.

Upvotes: 2

Related Questions