自动布局程序长宽比设置

时间:2019-01-21 08:59:11

标签: ios autolayout uikit ios-autolayout

我正在尝试添加一个子视图以查看和定义自动布局约束,包括纵横比。但是我在运行时看到的宽高比不是我在约束中定义的。我究竟做错了什么?正如您在代码中看到的那样,背景视图的高度应为背景视图宽度的0.5,但屏幕截图中的情况并非如此。这是我的代码:

class ViewController: UIViewController {

private var backgroundView:UIView?

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    view.backgroundColor = UIColor.orange

    backgroundView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 100))
    backgroundView?.backgroundColor = UIColor.black.withAlphaComponent(1.0)
    backgroundView?.layer.borderColor = UIColor.white.cgColor
    backgroundView?.layer.borderWidth = 1.5
    backgroundView?.layer.cornerRadius = 4
    backgroundView?.clipsToBounds = true
    backgroundView?.translatesAutoresizingMaskIntoConstraints = false

    view.addSubview(backgroundView!)

    backgroundView?.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1.0).isActive = true
    backgroundView?.heightAnchor.constraint(equalTo: backgroundView!.widthAnchor, multiplier: 0.5).isActive = true
    backgroundView?.centerXAnchor.constraint(equalTo: view!.centerXAnchor, constant: 0).isActive = true
    backgroundView?.topAnchor.constraint(equalTo: view!.topAnchor, constant: 4).isActive = true
}


 }

这是屏幕截图:

enter image description here

2 个答案:

答案 0 :(得分:1)

“背景视图的高度应为背景视图宽度的0.5”

您的屏幕截图大小为1334 x 750

您的backgroundView(包括边界)为1334 x 667

1334 * 0.5 == 667

因此,您正在获得所需的东西。

答案 1 :(得分:0)

尝试更改高度限制以进行设置:

library(shiny)

choices = c("","Employee","State","City","Status")

ui = fluidPage(
  box(
    uiOutput("InputUI"),

    actionButton(
      inputId = "ENTER",
      style = "simple",
      size = "sm",
      label="Print Number of Choices in Console"
    )
  )

)
server = function(input, output) {
  filterRow=reactiveValues(
    CurrentRow=4
  )

  output$InputUI=renderUI({
    box(
      div(
        selectInput(
          inputId=paste("FilterField",1,sep = "_"),
          label="",
          choices=choices,
          multiple=F,
          selectize = F
        )
      ),

      div(
        selectInput(
          inputId=paste("FilterField",2,sep = "_"),
          label="",
          choices=choices,
          multiple=F,
          selectize = F
        )
      ),

      div(
        selectInput(
          inputId=paste("FilterField",3,sep = "_"),
          label="",
          choices=choices,
          multiple=F,
          selectize = F
        )
      ),

      div(
        selectInput(
          inputId=paste("FilterField",4,sep = "_"),
          label="",
          choices=choices,
          multiple=F,
          selectize = F
        )
      )
    )

  })

  observeEvent(input$ENTER,{
     print(input[[ paste0("FilterField","_",1)]]["choices"])
  })

}
shinyApp(ui,server,options=list(launch.browser=F))

注意::您正在获得所需的确切结果。它的高度是宽度的一半。屏幕截图没有问题。