AutoLayout 적용

김서영·2023년 2월 2일
0

AutoLayout

목록 보기
2/3

iPhone 14 Pro

iPone SE (3th generation)

iPad (10th generation)

가로모드

View Controller

//
//  ViewController.swift
//  change30
//
//  Created by 소프트웨어컴퓨터 on 2022/12/13.
//

import UIKit

class ViewController: UIViewController {

    
    @IBOutlet weak var Nm: UILabel!
    
    @IBOutlet weak var weaImg: UIImageView!
    
    @IBOutlet weak var txtweather: UITextField!
    
    @IBOutlet weak var wtresult: UILabel!
    
    @IBAction func Celsius(_ sender: UIButton) {
        if(txtweather.text == ""){
            wtresult.textColor = UIColor.red
            wtresult.text = "온도를 입력하세요!!"
            return
        }
        wtresult.backgroundColor = UIColor.white
        wtresult.textColor = UIColor(displayP3Red: 0.0, green: 1.0, blue: 1.0, alpha: 1.0)
        let txtweather = Double(txtweather.text!)!
        let celsius = (txtweather-32)*5/9
        let shortenedcelsius = String(format: "%.1f", celsius)
        wtresult.clipsToBounds = true
        wtresult.layer.cornerRadius = 10
        wtresult.text = "섭씨 : \(shortenedcelsius)도 입니다."
        
    }
    
    @IBAction func Fahrenheit(_ sender: UIButton) {
        if(txtweather.text == ""){
            wtresult.textColor = UIColor.red
            wtresult.text = "온도를 입력하세요!!"
            return
        }
        wtresult.backgroundColor = UIColor.white
        wtresult.textColor = UIColor(displayP3Red: 1.0, green: 0.0, blue: 1.0, alpha: 1.0)
        let txtweather = Double(txtweather.text!)!
        let fahrenheit = txtweather*1.8+32
        let shortenedfahrenheit = String(format: "%.1f", fahrenheit)
        wtresult.clipsToBounds = true
        wtresult.layer.cornerRadius = 10
        wtresult.text = "화씨 : \(shortenedfahrenheit)도 입니다."
    }
    
    @IBAction func Onoff(_ sender: UISwitch) {
        
        if sender.isOn{
            txtweather.backgroundColor = UIColor(displayP3Red: 0.7, green: 0.7, blue: 0.7, alpha: 1.0)
        }
        else {
            txtweather.backgroundColor = UIColor.white
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        Nm.translatesAutoresizingMaskIntoConstraints = false
        weaImg.translatesAutoresizingMaskIntoConstraints = false
        txtweather.translatesAutoresizingMaskIntoConstraints = false
        wtresult.translatesAutoresizingMaskIntoConstraints = false
        
        
        
        NSLayoutConstraint.activate([
            
            Nm.widthAnchor.constraint(equalToConstant: 240),
            Nm.heightAnchor.constraint(equalToConstant: 40),
            Nm.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor, constant: 55),
            Nm.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor, constant: 80),
            Nm.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor, constant: -80),
            
            weaImg.widthAnchor.constraint(equalToConstant: 240),
            weaImg.heightAnchor.constraint(equalToConstant: 130),
            weaImg.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
            weaImg.topAnchor.constraint(equalTo: self.wtresult.bottomAnchor, constant: 15),
            
            txtweather.widthAnchor.constraint(equalToConstant: 240),
            txtweather.heightAnchor.constraint(equalToConstant: 40),
            txtweather.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor, constant: 110),
            txtweather.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor, constant: 70),
            txtweather.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor, constant: -70),
            
            wtresult.widthAnchor.constraint(equalToConstant: 240),
            wtresult.heightAnchor.constraint(equalToConstant: 40),
            wtresult.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor, constant: 250),
            wtresult.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor, constant: 90),
            wtresult.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor, constant: -90)
         ])
        
        
    }


}

PrepareViewController

import UIKit
import AVKit

class PrepareViewController: UIViewController {

    
    @IBOutlet weak var hott: UILabel!
    
    
    @IBOutlet weak var coldd: UILabel!
    
    
    @IBAction func playhot(_ sender: UIButton) {
        let file:String? = Bundle.main.path(forResource:"hotprepare", ofType: "mp4")
        let url = NSURL(fileURLWithPath: file!)
        let playerController = AVPlayerViewController()
        let player = AVPlayer(url: url as URL)
        playerController.player = player
        self.present(playerController, animated: true)
         
        player.play()    }
    
    @IBAction func playcold(_ sender: UIButton) {
        let file:String? = Bundle.main.path(forResource:"coldprepare", ofType: "mp4")
        let url = NSURL(fileURLWithPath: file!)
        let playerController = AVPlayerViewController()
        let player = AVPlayer(url: url as URL)
        playerController.player = player
        self.present(playerController, animated: true)
         
        player.play()    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        hott.translatesAutoresizingMaskIntoConstraints = false
        coldd.translatesAutoresizingMaskIntoConstraints = false
        
        
        
        NSLayoutConstraint.activate([
            
            hott.widthAnchor.constraint(equalToConstant: 200),
            hott.heightAnchor.constraint(equalToConstant: 85),
            hott.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.centerYAnchor, constant: -140),
            hott.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.centerXAnchor, constant: -100),
            
            
            coldd.widthAnchor.constraint(equalToConstant: 200),
            coldd.heightAnchor.constraint(equalToConstant: 85),
            coldd.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.centerYAnchor, constant: 235),
            coldd.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.centerXAnchor, constant: -100),
         ])
    }
    
}

iteViewController


import UIKit
import WebKit

class SiteViewController: UIViewController {

    @IBOutlet weak var webView: WKWebView!
    override func viewDidLoad() {
        super.viewDidLoad()
        guard let url = URL(string: "https://weather.naver.com")
        else{ return}
        let request = URLRequest(url: url)
        webView.load(request)    }
    

    @IBAction func Sgsite(_ sender: UISegmentedControl) {
        if sender.selectedSegmentIndex == 0{
            guard let url = URL(string: "https://weather.naver.com")
            else{ return}
            let request = URLRequest(url: url)
            webView.load(request)
        }
        else{
            guard let url = URL(string: "https://www.weather.go.kr/w/index.do")
            else{ return }
            let request = URLRequest(url: url)
            webView.load(request)        }
    }
    
}

소스에 없는 View들은 인터페이스를 통해 AutoLayout을 했습니다.

0개의 댓글