- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
添加旋转相关约束后,我的 UIScrollView 停止工作。我将两组约束放在两个数组中,每次旋转发生后,我激活其中一个并停用第二个。它在立即激活约束 (constraint.isActive = true) 之前起作用,并且只有一组。
func setupLayout() {
navigationItem.hidesBackButton = true
navigationController?.navigationBar.barTintColor = #colorLiteral(red: 0.1450980392, green: 0.1960784314, blue: 0.2509803922, alpha: 1)
setViewBackground()
let guide = self.view.safeAreaLayoutGuide
view.addSubview(scrollView)
scrollView.addSubview(contentView)
contentView.addSubview(topLeftViewContainer)
topLeftViewContainer.addSubview(idTextField)
topLeftViewContainer.addSubview(passTextField)
contentView.addSubview(bottomRightViewContainer)
bottomRightViewContainer.addSubview(logInButton)
bottomRightViewContainer.addSubview(registerButton)
// MARK: - Portrait Constraints -
//scrollView
portraitConstraints.append(scrollView.topAnchor.constraint(equalTo: guide.topAnchor))
scrollVievBottomConstraint = NSLayoutConstraint(item: scrollView, attribute: .bottom, relatedBy: .equal, toItem: guide, attribute: .bottom, multiplier: 1.0, constant: 0)
portraitConstraints.append(scrollVievBottomConstraint)
portraitConstraints.append(scrollView.leadingAnchor.constraint(equalTo: guide.leadingAnchor))
portraitConstraints.append(scrollView.trailingAnchor.constraint(equalTo: guide.trailingAnchor))
//contentView
portraitConstraints.append(contentView.topAnchor.constraint(equalTo: scrollView.topAnchor))
portraitConstraints.append(contentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor))
portraitConstraints.append(contentView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor))
portraitConstraints.append(contentView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor))
portraitConstraints.append(contentView.widthAnchor.constraint(equalTo: scrollView.widthAnchor))
//topLeftViewContainer
portraitConstraints.append(topLeftViewContainer.topAnchor.constraint(equalTo: contentView.topAnchor))
portraitConstraints.append(topLeftViewContainer.heightAnchor.constraint(equalTo: guide.heightAnchor, multiplier: 0.5))
portraitConstraints.append(topLeftViewContainer.leadingAnchor.constraint(equalTo: contentView.leadingAnchor))
portraitConstraints.append(topLeftViewContainer.trailingAnchor.constraint(equalTo: contentView.trailingAnchor))
//idTextField
portraitConstraints.append(idTextField.heightAnchor.constraint(equalToConstant: 20))
portraitConstraints.append(idTextField.widthAnchor.constraint(equalToConstant: 180))
portraitConstraints.append(idTextField.topAnchor.constraint(equalTo: topLeftViewContainer.topAnchor, constant: 50))
portraitConstraints.append(idTextField.centerXAnchor.constraint(equalTo: topLeftViewContainer.centerXAnchor))
//passTextField
portraitConstraints.append(passTextField.heightAnchor.constraint(equalToConstant: 20))
portraitConstraints.append(passTextField.widthAnchor.constraint(equalToConstant: 180))
portraitConstraints.append(passTextField.topAnchor.constraint(equalTo: idTextField.bottomAnchor, constant: 20))
portraitConstraints.append(passTextField.centerXAnchor.constraint(equalTo: topLeftViewContainer.centerXAnchor))
//bottomRightViewContainer
portraitConstraints.append(bottomRightViewContainer.heightAnchor.constraint(equalTo: guide.heightAnchor, multiplier: 0.5))
portraitConstraints.append(bottomRightViewContainer.topAnchor.constraint(equalTo: topLeftViewContainer.bottomAnchor))
portraitConstraints.append(bottomRightViewContainer.bottomAnchor.constraint(equalTo: contentView.bottomAnchor))
portraitConstraints.append(bottomRightViewContainer.leadingAnchor.constraint(equalTo: contentView.leadingAnchor))
portraitConstraints.append(bottomRightViewContainer.trailingAnchor.constraint(equalTo: contentView.trailingAnchor))
//logInButton
portraitConstraints.append(logInButton.heightAnchor.constraint(equalToConstant: 30))
portraitConstraints.append(logInButton.widthAnchor.constraint(equalToConstant: 120))
portraitConstraints.append(logInButton.topAnchor.constraint(lessThanOrEqualTo: bottomRightViewContainer.topAnchor, constant: 50))
portraitConstraints.append(logInButton.centerXAnchor.constraint(equalTo: bottomRightViewContainer.centerXAnchor))
//registerButton
portraitConstraints.append(registerButton.heightAnchor.constraint(equalToConstant: 30))
portraitConstraints.append(registerButton.widthAnchor.constraint(equalToConstant: 120))
portraitConstraints.append(registerButton.topAnchor.constraint(equalTo: logInButton.bottomAnchor, constant: 20))
portraitConstraints.append(registerButton.centerXAnchor.constraint(equalTo: bottomRightViewContainer.centerXAnchor))
// MARK: - Landscape Constraints -
//scrollView
landscapeConstraints.append(scrollView.topAnchor.constraint(equalTo: guide.topAnchor))
scrollVievBottomConstraint = NSLayoutConstraint(item: scrollView, attribute: .bottom, relatedBy: .equal, toItem: guide, attribute: .bottom, multiplier: 1.0, constant: 0)
landscapeConstraints.append(scrollVievBottomConstraint)
landscapeConstraints.append(scrollView.leadingAnchor.constraint(equalTo: guide.leadingAnchor))
landscapeConstraints.append(scrollView.trailingAnchor.constraint(equalTo: guide.trailingAnchor))
//contentView
landscapeConstraints.append(contentView.topAnchor.constraint(equalTo: scrollView.topAnchor))
landscapeConstraints.append(contentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor))
landscapeConstraints.append(contentView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor))
landscapeConstraints.append(contentView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor))
landscapeConstraints.append(contentView.widthAnchor.constraint(equalTo: scrollView.widthAnchor))
//topLeftViewContainer
landscapeConstraints.append(topLeftViewContainer.topAnchor.constraint(equalTo: contentView.topAnchor))
landscapeConstraints.append(topLeftViewContainer.widthAnchor.constraint(equalTo: guide.widthAnchor, multiplier: 0.5))
landscapeConstraints.append(topLeftViewContainer.leadingAnchor.constraint(equalTo: contentView.leadingAnchor))
landscapeConstraints.append(topLeftViewContainer.bottomAnchor.constraint(equalTo: guide.bottomAnchor))
//idTextField
landscapeConstraints.append(idTextField.heightAnchor.constraint(equalToConstant: 20))
landscapeConstraints.append(idTextField.widthAnchor.constraint(equalToConstant: 180))
landscapeConstraints.append(idTextField.topAnchor.constraint(equalTo: topLeftViewContainer.centerYAnchor, constant: -30))
landscapeConstraints.append(idTextField.centerXAnchor.constraint(equalTo: topLeftViewContainer.centerXAnchor))
//passTextField
landscapeConstraints.append(passTextField.heightAnchor.constraint(equalToConstant: 20))
landscapeConstraints.append(passTextField.widthAnchor.constraint(equalToConstant: 180))
landscapeConstraints.append(passTextField.topAnchor.constraint(equalTo: idTextField.bottomAnchor, constant: 20))
landscapeConstraints.append(passTextField.centerXAnchor.constraint(equalTo: topLeftViewContainer.centerXAnchor))
//bottomRightViewContainer
landscapeConstraints.append(bottomRightViewContainer.widthAnchor.constraint(equalTo: guide.widthAnchor, multiplier: 0.5))
landscapeConstraints.append(bottomRightViewContainer.topAnchor.constraint(equalTo: contentView.topAnchor))
landscapeConstraints.append(bottomRightViewContainer.bottomAnchor.constraint(equalTo: guide.bottomAnchor))
landscapeConstraints.append(bottomRightViewContainer.trailingAnchor.constraint(equalTo: contentView.trailingAnchor))
//logInButton
landscapeConstraints.append(logInButton.heightAnchor.constraint(equalToConstant: 30))
landscapeConstraints.append(logInButton.widthAnchor.constraint(equalToConstant: 120))
landscapeConstraints.append(logInButton.topAnchor.constraint(lessThanOrEqualTo: bottomRightViewContainer.centerYAnchor, constant: -30))
landscapeConstraints.append(logInButton.centerXAnchor.constraint(equalTo: bottomRightViewContainer.centerXAnchor))
//registerButton
landscapeConstraints.append(registerButton.heightAnchor.constraint(equalToConstant: 30))
landscapeConstraints.append(registerButton.widthAnchor.constraint(equalToConstant: 120))
landscapeConstraints.append(registerButton.topAnchor.constraint(equalTo: logInButton.bottomAnchor, constant: 20))
landscapeConstraints.append(registerButton.centerXAnchor.constraint(equalTo: bottomRightViewContainer.centerXAnchor))
NotificationCenter.default.addObserver(self, selector: #selector(keyBoardWillShow(notification:)), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyBoardWillHide(notification:)), name: .UIKeyboardWillHide, object: nil)
NSLayoutConstraint.activate(portraitConstraints)
}
// MARK: - Keyboard Handling
@objc func keyBoardWillShow(notification: NSNotification){
let userInfo:NSDictionary = notification.userInfo! as NSDictionary
let keyboardFrame:NSValue = userInfo.value(forKey: UIKeyboardFrameEndUserInfoKey) as! NSValue
let keyboardRectangle = keyboardFrame.cgRectValue
let keyboardHeight = keyboardRectangle.height - view.safeAreaInsets.bottom
scrollVievBottomConstraint.constant = -keyboardHeight
scrollView.setNeedsUpdateConstraints()
self.view.layoutIfNeeded()
print("SHOW KEYBOARD")
}
@objc func keyBoardWillHide(notification: NSNotification){
scrollVievBottomConstraint.constant = 0
scrollView.setNeedsUpdateConstraints()
self.view.layoutIfNeeded()
print("HIDE KEYBOARD")
}
// MARK: - Orientation -
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
if UIDevice.current.orientation.isLandscape {
NSLayoutConstraint.deactivate(portraitConstraints)
NSLayoutConstraint.activate(landscapeConstraints)
} else {
NSLayoutConstraint.deactivate(landscapeConstraints)
NSLayoutConstraint.activate(portraitConstraints)
}
}
最佳答案
也许问题出在这行:
//topLeftViewContainer
landscapeConstraints.append(topLeftViewContainer.topAnchor.constraint(equalTo: contentView.topAnchor))
landscapeConstraints.append(topLeftViewContainer.widthAnchor.constraint(equalTo: guide.widthAnchor, multiplier: 0.5))
landscapeConstraints.append(topLeftViewContainer.leadingAnchor.constraint(equalTo: contentView.leadingAnchor))
landscapeConstraints.append(topLeftViewContainer.bottomAnchor.constraint(equalTo: guide.bottomAnchor))
guide.bottomAnchor 还是 contentView.bottomAnchor?
//底部右 View 容器
landscapeConstraints.append(bottomRightViewContainer.widthAnchor.constraint(equalTo: guide.widthAnchor, multiplier: 0.5))
landscapeConstraints.append(bottomRightViewContainer.topAnchor.constraint(equalTo: contentView.topAnchor))
landscapeConstraints.append(bottomRightViewContainer.bottomAnchor.constraint(equalTo: guide.bottomAnchor))
landscapeConstraints.append(bottomRightViewContainer.trailingAnchor.constraint(equalTo: contentView.trailingAnchor))
guide.widthAnchor 和 guide.bottomAnchor 还是 contentView.widthAnchor 和 contentView.bottomAnchor?
关于swift - 更改约束后 constUIScrollView 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51726005/
我可以添加一个检查约束来确保所有值都是唯一的,但允许默认值重复吗? 最佳答案 您可以使用基于函数的索引 (FBI) 来实现此目的: create unique index idx on my_tabl
嗨,我在让我的约束在grails项目中工作时遇到了一些麻烦。我试图确保Site_ID的字段不留为空白,但仍接受空白输入。另外,我尝试设置字段显示的顺序,但即使尝试时也无法反射(reflect)在页面上
我似乎做错了,我正在尝试将一个字段修改为外键,并使用级联删除...我做错了什么? ALTER TABLE my_table ADD CONSTRAINT $4 FOREIGN KEY my_field
阅读目录 1、约束的基本概念 2、约束的案例实践 3、外键约束介绍 4、外键约束展示 5、删除
SQLite 约束 约束是在表的数据列上强制执行的规则。这些是用来限制可以插入到表中的数据类型。这确保了数据库中数据的准确性和可靠性。 约束可以是列级或表级。列级约束仅适用于列,表级约束被应用到整
我在 SerenityOS project 中偶然发现了这段代码: template void dbgln(CheckedFormatString&& fmtstr, const Parameters
我有表 tariffs,有两列:(tariff_id, reception) 我有表 users,有两列:(user_id, reception) 我的表 users_tariffs 有两列:(use
在 Derby 服务器中,如何使用模式的系统表中的信息来创建选择语句以检索每个表的约束名称? 最佳答案 相关手册是Derby Reference Manual .有许多可用版本:10.13 是 201
我正在使用 z3py 进行编码。请参阅以下示例。 from z3 import * x = Int('x') y = Int('y') s = Solver() s.add(x+y>3) if s.c
非常快速和简单的问题。我正在运行一个脚本来导入数据并声明了一个临时表并将检查约束应用于该表。显然,如果脚本运行不止一次,我会检查临时表是否已经存在,如果存在,我会删除并重新创建临时表。这也会删除并重新
我有一个浮点变量 x在一个线性程序中,它应该是 0或两个常量之间 CONSTANT_A和 CONSTANT_B : LP.addConstraint(x == 0 OR CONSTANT_A <= x
我在使用grails的spring-data-neo4j获得唯一约束时遇到了一些麻烦。 我怀疑这是因为我没有正确连接它,但是存储库正在扫描和连接,并且CRUD正在工作,所以我不确定我做错了什么。 我正
这个问题在这里已经有了答案: Is there a constraint that restricts my generic method to numeric types? (24 个回答) 7年前
我有一个浮点变量 x在一个线性程序中,它应该是 0或两个常量之间 CONSTANT_A和 CONSTANT_B : LP.addConstraint(x == 0 OR CONSTANT_A <= x
在iOS的 ScrollView 中将图像和带有动态文本(动态高度)的标签居中的最佳方法是什么? 我必须添加哪些约束?我真的无法弄清楚它是如何工作的,也许我无法处理它,因为我是一名 Android 开
考虑以下代码: class Foo f class Bar b newtype D d = D call :: Proxy c -> (forall a . c a => a -> Bool) ->
我有一个类型类,它强加了 KnownNat约束: class KnownNat (Card a) => HasFin a where type Card a :: Nat ... 而且,我有几
我知道REST原则上与HTTP无关。 HTTP是协议,REST是用于通过Web传输hypermedia的体系结构样式。 REST可以使用诸如HTTP,FTP等的任何应用程序层协议。关于REST的讨论很
我有这样的情况,我必须在数据库中存储复杂的数据编号。类似于 21/2011,其中 21 是文件编号,但 2011 是文件年份。所以我需要一些约束来处理唯一性,因为有编号为 21/2010 和 21/2
我有一个 MySql (InnoDb) 表,表示对许多类型的对象之一所做的评论。因为我正在使用 Concrete Table Inheritance ,对于下面显示的每种类型的对象(商店、类别、项目)
我是一名优秀的程序员,十分优秀!