Created
October 9, 2019 06:25
-
-
Save Haeuncs/df145544b06636cffe90affafc0cc229 to your computer and use it in GitHub Desktop.
scrollView InsideView Height
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// testProj | |
// | |
// Created by LEE HAEUN on 09/10/2019. | |
// Copyright © 2019 LEE HAEUN. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
var insideViewHeight: NSLayoutConstraint? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view. | |
initView() | |
} | |
func initView(){ | |
view.addSubview(mainView) | |
mainView.addSubview(changeHeightBtn) | |
mainView.addSubview(scrollView) | |
scrollView.addSubview(insideView) | |
// mainView가 화면에 딱 맞게 채워짐 | |
// scrollView는 mainView의 중앙에 있음 | |
NSLayoutConstraint.activate([ | |
mainView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor), | |
mainView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor), | |
mainView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor), | |
mainView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor), | |
changeHeightBtn.topAnchor.constraint(equalTo: mainView.topAnchor), | |
changeHeightBtn.widthAnchor.constraint(equalToConstant: 200), | |
changeHeightBtn.heightAnchor.constraint(equalToConstant: 40), | |
changeHeightBtn.centerXAnchor.constraint(equalTo: mainView.centerXAnchor), | |
scrollView.topAnchor.constraint(equalTo: changeHeightBtn.bottomAnchor, constant: 10), | |
scrollView.leadingAnchor.constraint(equalTo: mainView.leadingAnchor, constant: 10), | |
scrollView.trailingAnchor.constraint(equalTo: mainView.trailingAnchor, constant: -10), | |
scrollView.bottomAnchor.constraint(equalTo: mainView.bottomAnchor, constant: -10), | |
insideView.topAnchor.constraint(equalTo: scrollView.topAnchor), | |
insideView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor), | |
insideView.widthAnchor.constraint(equalTo: scrollView.widthAnchor), | |
insideView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor), | |
]) | |
insideViewHeight = insideView.heightAnchor.constraint(equalToConstant: 100) | |
insideViewHeight?.isActive = true | |
} | |
lazy var changeHeightBtn: UIButton = { | |
let button = UIButton() | |
button.backgroundColor = .blue | |
button.translatesAutoresizingMaskIntoConstraints = false | |
button.addTarget(nil, action: #selector(changeInsideViewHeight), for: .touchUpInside) | |
button.setTitle("높이를 바꿉니다", for: .normal) | |
return button | |
}() | |
lazy var mainView: UIView = { | |
let view = UIView() | |
view.translatesAutoresizingMaskIntoConstraints = false | |
return view | |
}() | |
lazy var insideView: UIView = { | |
let view = UIView() | |
view.translatesAutoresizingMaskIntoConstraints = false | |
view.backgroundColor = .yellow | |
return view | |
}() | |
lazy var scrollView: UIScrollView = { | |
let view = UIScrollView() | |
view.translatesAutoresizingMaskIntoConstraints = false | |
view.backgroundColor = .red | |
return view | |
}() | |
} | |
extension ViewController { | |
@objc func changeInsideViewHeight() { | |
if insideViewHeight?.constant == 100 { | |
insideViewHeight?.constant = 1000 | |
} | |
else { | |
insideViewHeight?.constant = 100 | |
} | |
insideViewHeight?.isActive = true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment