Swift 3中如何将XIB视图全屏替换添加到视图控制器?
Solution: Make TranslateView Full Screen Over Camera View
To get your custom XIB view to fully cover the camera session view when the button is tapped, you just need to adjust how you set up the TranslateView's frame and autoresizing behavior in the button tap handler. Here's the updated Swift 3 code:
1. Keep your existing TranslateView implementation (it's already correctly set up)
import UIKit class TranslateView: UIView { @IBOutlet var TranslateView: UIView! override init(frame: CGRect){ super.init(frame: frame) commonInit() } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) commonInit() } private func commonInit(){ Bundle.main.loadNibNamed("TranslateView", owner: self, options: nil) addSubview(TranslateView) TranslateView.frame = self.bounds TranslateView.autoresizingMask = [.flexibleHeight, .flexibleWidth] } }
2. Updated ViewController Code (focus on the pressed method)
import UIKit import AVFoundation import Vision class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate { let captureSession = AVCaptureSession() let button: UIButton = { let button = UIButton() button.addTarget(self, action: #selector(pressed(sender:)), for: .touchUpInside) button.backgroundColor = UIColor.clear button.setTitle("Translate", for: .normal) button.translatesAutoresizingMaskIntoConstraints = false // Optional: Make button visible over camera feed button.setTitleColor(.white, for: .normal) button.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: .semibold) return button }() override func viewDidLoad() { super.viewDidLoad() setupCaptureSession() view.addSubview(button) // Position button at bottom-center NSLayoutConstraint.activate([ button.centerXAnchor.constraint(equalTo: view.centerXAnchor), button.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -50) ]) } @objc func pressed(sender: UIButton!) { button.removeFromSuperview() guard let headerView = Bundle.main.loadNibNamed("TranslateView", owner: self, options: nil)?.first as? TranslateView else { print("Failed to load TranslateView from XIB") return } // Option 1: Frame-based layout (simple for full screen) headerView.frame = self.view.bounds headerView.autoresizingMask = [.flexibleWidth, .flexibleHeight] // Option 2: Auto layout constraints (uncomment below if preferred) /* headerView.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ headerView.topAnchor.constraint(equalTo: view.topAnchor), headerView.leadingAnchor.constraint(equalTo: view.leadingAnchor), headerView.trailingAnchor.constraint(equalTo: view.trailingAnchor), headerView.bottomAnchor.constraint(equalTo: view.bottomAnchor) ]) */ self.view.addSubview(headerView) } // Your camera session setup (added a basic working skeleton) func setupCaptureSession() { guard let captureDevice = AVCaptureDevice.default(for: .video) else { return } do { let input = try AVCaptureDeviceInput(device: captureDevice) if captureSession.canAddInput(input) { captureSession.addInput(input) } let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) previewLayer.frame = view.bounds previewLayer.videoGravity = .resizeAspectFill view.layer.addSublayer(previewLayer) captureSession.startRunning() } catch { print(error.localizedDescription) } } // Required delegate method (empty implementation for completeness) func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) { // Add your frame processing logic here } }
Key Fixes & Improvements:
- Replaced the fixed 50-height frame with
self.view.boundsto makeTranslateViewfill the entire screen - Added
autoresizingMaskto ensure the view adapts to screen rotations or size changes - Included an optional auto layout approach for more flexible layout control
- Added a safety check with
guardto handle XIB loading failures gracefully - Improved button visibility and positioning over the camera feed
内容的提问来源于stack exchange,提问作者Sieuwe Elferink
相关产品推荐
相关产品推荐

