我正在使用swift 5构建适用于iPhone和iPad的应用程序,但遇到一些约束方面的问题。
首先,我创建了所有需要的视图,堆栈视图和标签。
let st = UIStackView()
let st1 = UIStackView()
let st2 = UIStackView()
let underConstructionStackView = UIStackView()
let scheduleJobsStackView = UIStackView()
let withoutScheduleStackView = UIStackView()
let withoutPMStackView = UIStackView()
var underConstruction = PieChartView()
var scheduleJobs = PieChartView()
var withoutSchedule = PieChartView()
var withoutPM = PieChartView()
var underConstructionLabel = UILabel()
var scheduleJobsLabel = UILabel()
var withoutScheduleLabel = UILabel()
var withoutPMLabel = UILabel()
然后,将所有这些项目的translatesAutoresizingMaskIntoConstraints设置为false:
st.translatesAutoresizingMaskIntoConstraints = false
underConstruction.translatesAutoresizingMaskIntoConstraints = false
scheduleJobs.translatesAutoresizingMaskIntoConstraints = false
withoutSchedule.translatesAutoresizingMaskIntoConstraints = false
withoutPM.translatesAutoresizingMaskIntoConstraints = false
underConstructionLabel.translatesAutoresizingMaskIntoConstraints = false
scheduleJobsLabel.translatesAutoresizingMaskIntoConstraints = false
withoutScheduleLabel.translatesAutoresizingMaskIntoConstraints = false
withoutPMLabel.translatesAutoresizingMaskIntoConstraints = false
然后我设置堆栈视图方向并将其添加到视图中
let size = UIScreen.main.bounds.size
if size.width < size.height {
st.axis = .horizontal
st1.axis = .vertical
st2.axis = .vertical
} else {
st.axis = .horizontal
st1.axis = .horizontal
st2.axis = .horizontal
}
underConstructionStackView.axis = .vertical
scheduleJobsStackView.axis = .vertical
withoutScheduleStackView.axis = .vertical
withoutPMStackView.axis = .vertical
st.distribution = .fill
st.alignment = .center
view.addSubview(st)
st.addArrangedSubview(st1)
st.addArrangedSubview(st2)
underConstructionStackView.addArrangedSubview(underConstructionLabel)
underConstructionStackView.addArrangedSubview(underConstruction)
st1.addArrangedSubview(underConstructionStackView)
scheduleJobsStackView.addArrangedSubview(scheduleJobsLabel)
scheduleJobsStackView.addArrangedSubview(scheduleJobs)
st1.addArrangedSubview(scheduleJobsStackView)
withoutScheduleStackView.addArrangedSubview(withoutScheduleLabel)
withoutScheduleStackView.addArrangedSubview(withoutSchedule)
st2.addArrangedSubview(withoutScheduleStackView)
withoutPMStackView.addArrangedSubview(withoutPMLabel)
withoutPMStackView.addArrangedSubview(withoutPM)
st2.addArrangedSubview(withoutPMStackView)
st1.distribution = .fillEqually
st2.distribution = .fillEqually
然后,我以编程方式创建了约束:
NSLayoutConstraint.activate([
st.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
st.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
st.topAnchor.constraint(equalTo: self.communityButton.topAnchor,constant:60),
underConstruction.widthAnchor.constraint(equalTo: underConstruction.heightAnchor),
scheduleJobs.widthAnchor.constraint(equalTo: scheduleJobs.heightAnchor),
withoutSchedule.widthAnchor.constraint(equalTo: withoutSchedule.heightAnchor),
withoutPM.widthAnchor.constraint(equalTo: withoutPM.heightAnchor),
])
我的问题是PieChartView(),
在iPad上看起来不错-横向
(附屏幕快照)
为了使PieChartView适应self.communityButton和PieChartView下面的网格之间的约束,我需要怎么做?
我尝试将UIStackView的底部约束设置为网格topAnchor的顶部,但是网格是Shinobigrid且没有topAnchor ....我该怎么办?
更新:
我尝试了以下
if size.width < size.height
{
if UIDevice.current.userInterfaceIdiom == .pad {
widthCon = st.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width * 1)
}
else
{
widthCon = st.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width * 0.95)
}
}
else
{
if UIDevice.current.userInterfaceIdiom == .pad {
widthCon = st.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width * 0.75)
}
else
{
widthCon = st.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width * 0.75)
}
}
widthCon.isActive = true
在初始负载下,一切看起来都很不错.....但是,当我旋转设备时,一切都变得混乱了。
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
if UIDevice.current.userInterfaceIdiom == .pad {
if UIDevice.current.orientation.isPortrait {
widthCon = st.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width * 0.75)
}
else {
widthCon = st.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width * 1)
}
}
else {
if UIDevice.current.orientation.isPortrait {
widthCon = st.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width * 0.50)
}
else{
widthCon = st.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width * 0.75)
}
}
self.view.layoutIfNeeded()
}
最佳答案
问题在这里
st.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
st.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
当它填满所有设备的屏幕时,您需要替换为
var widthCon:NSLayoutConstraint!
//put inside activate
st.centerXAnchor.constraint(equalTo: self.view.centerXAnchor)
// outside activate
widthCon = st.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width * 0.8)
widthCon.isActive = true
然后根据组合需要检查一下
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
if UIDevice.current.userInterfaceIdiom == .pad {
if UIDevice.current.orientation.isPortrait {
//
widthCon.constant = size.width * 0.5 // lower it
}
else {
//
}
}
else {
if UIDevice.current.orientation.isPortrait {
//
}
else{
//
}
}
self.view.layoutIfNeeded()
}
关于ios - 在以编程方式创建约束时遇到麻烦,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55937541/