在以编程方式创建约束时遇到麻烦

时间:2019-05-01 14:04:04

标签: ios swift constraints

我正在使用swift 5构建适用于iPhone和iPad的应用程序,但遇到一些约束方面的问题。

首先,我创建了所有需要的视图,堆栈视图和标签。

apache poi 4.1.0

然后我将所有这些项目的translationsAutoresizingMaskIntoConstraints设置为false:

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()

然后我设置堆栈视图方向并将其添加到视图

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

我的问题是PieChartView(),

  • iPhone-景观-PieChartView必须较小
  • iPhone-肖像-PieChartView可能更小
  • iPad-肖像-PieChartView太大了

这在iPad上看起来不错-横向

(附有屏幕截图)

要使PieChartView适应self.communityButton和PieChartView下面的网格之间的约束,我需要做什么?

我尝试将UIStackView的底部约束设置为网格topAnchor的顶部,但是网格是Shinobigrid且没有topAnchor .....我该怎么办?

enter image description here enter image description here enter image description here enter image description here

更新:

我尝试了以下

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),
])

在初始负载下,一切看起来都很不错.....但是,当我旋转设备时,一切都变得混乱了。

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

1 个答案:

答案 0 :(得分:2)

问题在这里

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()

}