如何以编程方式调整 swift 中的视图大小?
How to resize the view in swift programmatically?
我正在尝试调整视图控制器中的视图大小。按下按钮时视图的默认值为 60 我更改视图的高度。当我在调试器模式下检查它时,它会显示新值的高度,但在几行之后或在其他函数中它会自动设置回默认值 60 。
@IBAction func confirmButtonWasPressed(_ sender: Any) {
if confirmButton.titleLabel?.text == "Confirm pickup location" {
if !(pickupLocationField.text?.isEmpty ?? false) && originLocation != nil {
let frame = CGRect(x: searchesShadowView.frame.origin.x, y: searchesShadowView.frame.origin.y, width: searchesShadowView.frame.width, height: 115)
self.searchesShadowView.frame = frame
var imageView = UIImageView();
var image = UIImage(named: "work25");
imageView.image = image;
dropoffLocationField.rightView = imageView;
dropoffLocationField.rightViewMode = UITextField.ViewMode.always
}
else {
showToast(with: "Select pick up location")
}
}
}
我用的另一种方式
@IBAction func confirmButtonWasPressed(_ sender: Any) {
if confirmButton.titleLabel?.text == "Confirm pickup location" {
if !(pickupLocationField.text?.isEmpty ?? false) && originLocation != nil {
searchesShadowView.frame.size.height = 115
var imageView = UIImageView();
var image = UIImage(named: "work25");
imageView.image = image;
dropoffLocationField.rightView = imageView;
dropoffLocationField.rightViewMode = UITextField.ViewMode.always
}
else {
showToast(with: "Select pick up location")
}
}
}
还有这个searchesShadowView.heightAnchor.constraint(equalToConstant: 115).isActive = true
首先你应该从 Interface Builder
连接高度限制的 IBOutlet
然后你可以像这样更改高度限制的 constant
->
@IBAction func confirmButtonWasPressed(_ sender: Any) {
if confirmButton.titleLabel?.text == "Confirm pickup location" {
if !(pickupLocationField.text?.isEmpty ?? false) && originLocation != nil {
// You can change the value of the constraint
searchesShadowHeightConstraint.constant = 115
var imageView = UIImageView()
var image = UIImage(named: "work25")
imageView.image = image
dropoffLocationField.rightView = imageView
dropoffLocationField.rightViewMode = .always
} else {
showToast(with: "Select pick up location")
}
// Then you should update the layout
view.layoutIfNeeded()
}
您必须创建 searchShadowView 高度限制的 IBOutlet。
searchViewheightConstaints.constant = yourRequiredHeight
//如果您是从代码创建的,请使用
searchesShadowView.translatesAutoresizingMaskIntoConstraints = false
您需要限制搜索 ShadowView 的高度。
@IBAction func confirmButtonWasPressed(_ sender: Any) {
if confirmButton.titleLabel?.text == "Confirm pickup location" {
if !(pickupLocationField.text?.isEmpty ?? false) && originLocation != nil {
let frame = CGRect(x: searchesShadowView.frame.origin.x, y: searchesShadowView.frame.origin.y, width: searchesShadowView.frame.width, height: 115)
DispatchQueue.main.async {
self.heightConstrain.constant = 10
self.view.layoutIfNeeded()
}
self.searchesShadowView.frame = frame
var imageView = UIImageView();
var image = UIImage(named: "work25");
imageView.image = image;
dropoffLocationField.rightView = imageView;
dropoffLocationField.rightViewMode = UITextField.ViewMode.always
}
else {
showToast(with: "Select pick up location")
}
}
}
尝试使用下面的代码。
import UIKit
class MyViewController: UIViewController {
let searchesShadowView = UIView()
let plusButton = UIButton()
let minusButton = UIButton()
var heightLayoutConstraint:NSLayoutConstraint?
let value: CGFloat = 10.0
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
init() {
super.init(nibName: nil, bundle: nil)
self.view.backgroundColor = .yellow
loadUIView()
loadPlusButton(plusButton)
loadMinusButton(minusButton)
}
func loadUIView() {
self.view.addSubview(searchesShadowView)
searchesShadowView.backgroundColor = .red
searchesShadowView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint(item: searchesShadowView, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1, constant: 200).isActive = true
NSLayoutConstraint(item: searchesShadowView, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1, constant: 50).isActive = true
NSLayoutConstraint(item: searchesShadowView, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailing, multiplier: 1, constant: -50).isActive = true
heightLayoutConstraint = NSLayoutConstraint(item: searchesShadowView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 200)
heightLayoutConstraint?.isActive = true
}
func loadPlusButton(_ button: UIButton) {
self.view.addSubview(button)
button.tag = 1
button.backgroundColor = UIColor.green
button.setTitle("+", for: .normal)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
button.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint(item: button, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1, constant: 100).isActive = true
NSLayoutConstraint(item: button, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1, constant: 50).isActive = true
NSLayoutConstraint(item: button, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant:
80).isActive = true
NSLayoutConstraint(item: button, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 30).isActive = true
}
func loadMinusButton(_ button: UIButton) {
self.view.addSubview(button)
button.tag = 0
button.backgroundColor = UIColor.green
button.setTitle("-", for: .normal)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
button.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint(item: button, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1, constant: 100).isActive = true
NSLayoutConstraint(item: button, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailing, multiplier: 1, constant: -50).isActive = true
NSLayoutConstraint(item: button, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant:
80).isActive = true
NSLayoutConstraint(item: button, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 30).isActive = true
}
@objc func buttonAction(sender: UIButton!) {
UIView.animate(withDuration: 0, delay: 0.2, options: UIView.AnimationOptions.curveEaseOut, animations: {
if sender.tag == 1 {
self.heightLayoutConstraint?.constant += self.value
}else{
self.heightLayoutConstraint?.constant -= ((self.heightLayoutConstraint?.constant ?? 0)-self.value) >= 0 ? self.value : 0
}
self.view.layoutIfNeeded()
}) { (value) in }
}
}
我正在尝试调整视图控制器中的视图大小。按下按钮时视图的默认值为 60 我更改视图的高度。当我在调试器模式下检查它时,它会显示新值的高度,但在几行之后或在其他函数中它会自动设置回默认值 60 。
@IBAction func confirmButtonWasPressed(_ sender: Any) {
if confirmButton.titleLabel?.text == "Confirm pickup location" {
if !(pickupLocationField.text?.isEmpty ?? false) && originLocation != nil {
let frame = CGRect(x: searchesShadowView.frame.origin.x, y: searchesShadowView.frame.origin.y, width: searchesShadowView.frame.width, height: 115)
self.searchesShadowView.frame = frame
var imageView = UIImageView();
var image = UIImage(named: "work25");
imageView.image = image;
dropoffLocationField.rightView = imageView;
dropoffLocationField.rightViewMode = UITextField.ViewMode.always
}
else {
showToast(with: "Select pick up location")
}
}
}
我用的另一种方式
@IBAction func confirmButtonWasPressed(_ sender: Any) {
if confirmButton.titleLabel?.text == "Confirm pickup location" {
if !(pickupLocationField.text?.isEmpty ?? false) && originLocation != nil {
searchesShadowView.frame.size.height = 115
var imageView = UIImageView();
var image = UIImage(named: "work25");
imageView.image = image;
dropoffLocationField.rightView = imageView;
dropoffLocationField.rightViewMode = UITextField.ViewMode.always
}
else {
showToast(with: "Select pick up location")
}
}
}
还有这个searchesShadowView.heightAnchor.constraint(equalToConstant: 115).isActive = true
首先你应该从 Interface Builder
连接高度限制的 IBOutlet
然后你可以像这样更改高度限制的 constant
->
@IBAction func confirmButtonWasPressed(_ sender: Any) {
if confirmButton.titleLabel?.text == "Confirm pickup location" {
if !(pickupLocationField.text?.isEmpty ?? false) && originLocation != nil {
// You can change the value of the constraint
searchesShadowHeightConstraint.constant = 115
var imageView = UIImageView()
var image = UIImage(named: "work25")
imageView.image = image
dropoffLocationField.rightView = imageView
dropoffLocationField.rightViewMode = .always
} else {
showToast(with: "Select pick up location")
}
// Then you should update the layout
view.layoutIfNeeded()
}
您必须创建 searchShadowView 高度限制的 IBOutlet。
searchViewheightConstaints.constant = yourRequiredHeight
//如果您是从代码创建的,请使用
searchesShadowView.translatesAutoresizingMaskIntoConstraints = false
您需要限制搜索 ShadowView 的高度。
@IBAction func confirmButtonWasPressed(_ sender: Any) {
if confirmButton.titleLabel?.text == "Confirm pickup location" {
if !(pickupLocationField.text?.isEmpty ?? false) && originLocation != nil {
let frame = CGRect(x: searchesShadowView.frame.origin.x, y: searchesShadowView.frame.origin.y, width: searchesShadowView.frame.width, height: 115)
DispatchQueue.main.async {
self.heightConstrain.constant = 10
self.view.layoutIfNeeded()
}
self.searchesShadowView.frame = frame
var imageView = UIImageView();
var image = UIImage(named: "work25");
imageView.image = image;
dropoffLocationField.rightView = imageView;
dropoffLocationField.rightViewMode = UITextField.ViewMode.always
}
else {
showToast(with: "Select pick up location")
}
}
}
尝试使用下面的代码。
import UIKit
class MyViewController: UIViewController {
let searchesShadowView = UIView()
let plusButton = UIButton()
let minusButton = UIButton()
var heightLayoutConstraint:NSLayoutConstraint?
let value: CGFloat = 10.0
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
init() {
super.init(nibName: nil, bundle: nil)
self.view.backgroundColor = .yellow
loadUIView()
loadPlusButton(plusButton)
loadMinusButton(minusButton)
}
func loadUIView() {
self.view.addSubview(searchesShadowView)
searchesShadowView.backgroundColor = .red
searchesShadowView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint(item: searchesShadowView, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1, constant: 200).isActive = true
NSLayoutConstraint(item: searchesShadowView, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1, constant: 50).isActive = true
NSLayoutConstraint(item: searchesShadowView, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailing, multiplier: 1, constant: -50).isActive = true
heightLayoutConstraint = NSLayoutConstraint(item: searchesShadowView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 200)
heightLayoutConstraint?.isActive = true
}
func loadPlusButton(_ button: UIButton) {
self.view.addSubview(button)
button.tag = 1
button.backgroundColor = UIColor.green
button.setTitle("+", for: .normal)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
button.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint(item: button, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1, constant: 100).isActive = true
NSLayoutConstraint(item: button, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1, constant: 50).isActive = true
NSLayoutConstraint(item: button, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant:
80).isActive = true
NSLayoutConstraint(item: button, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 30).isActive = true
}
func loadMinusButton(_ button: UIButton) {
self.view.addSubview(button)
button.tag = 0
button.backgroundColor = UIColor.green
button.setTitle("-", for: .normal)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
button.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint(item: button, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1, constant: 100).isActive = true
NSLayoutConstraint(item: button, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailing, multiplier: 1, constant: -50).isActive = true
NSLayoutConstraint(item: button, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant:
80).isActive = true
NSLayoutConstraint(item: button, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 30).isActive = true
}
@objc func buttonAction(sender: UIButton!) {
UIView.animate(withDuration: 0, delay: 0.2, options: UIView.AnimationOptions.curveEaseOut, animations: {
if sender.tag == 1 {
self.heightLayoutConstraint?.constant += self.value
}else{
self.heightLayoutConstraint?.constant -= ((self.heightLayoutConstraint?.constant ?? 0)-self.value) >= 0 ? self.value : 0
}
self.view.layoutIfNeeded()
}) { (value) in }
}
}