【问题标题】:Programmatically change the height and width of a UIImageView Xcode Swift以编程方式更改 UIImageView Xcode Swift 的高度和宽度
【发布时间】:2015-07-10 16:30:36
【问题描述】:

嘿,由于某种原因,我正在努力尝试设置我的一个图像视图的高度和宽度。我想设置它,这样高度只占我屏幕的 20%。我知道要定期设置它,您可以执行以下操作: 图像 = (0,0,50,50)

但我需要高度不是静态数字。 像 image = (0,0, frame.height * 0.2, 50)

有什么建议吗?

【问题讨论】:

    标签: ios xcode swift uiimageview


    【解决方案1】:

    Swift 3 中公认的答案:

    let screenSize: CGRect = UIScreen.main.bounds
    image.frame = CGRect(x: 0, y: 0, width: 50, height: screenSize.height * 0.2)
    

    【讨论】:

    • 人谢谢你屏幕的东西修复了 imageView 不遵守 .scaleAspectFit 规则
    【解决方案2】:
    let screenSize: CGRect = UIScreen.mainScreen().bounds
    image.frame = CGRectMake(0,0, screenSize.height * 0.2, 50)
    

    【讨论】:

    • 你也可以这样写上面的代码image.frame = CGRect(0,0, screenSize.height * 0.2, 50) 不同的是这样看起来更快捷。 CGRectMake 是 Objective-C 风格
    【解决方案3】:

    使用自动查看

    image.heightAnchor.constraint(equalToConstant: CGFloat(8)).isActive = true
    

    【讨论】:

    • 这对我来说是一个合适的答案
    【解决方案4】:

    嘿,我很快就知道了。由于某种原因,我只是在放屁。

    image.frame = CGRectMake(0 , 0, self.view.frame.width, self.view.frame.height * 0.2)
    

    【讨论】:

      【解决方案5】:

      更改UIView 的高度和/或宽度的更快/替代方法是通过frame.size 设置其宽度/高度:

      let neededHeight = UIScreen.main.bounds.height * 0.2
      yourView.frame.size.height = neededHeight
      

      【讨论】:

        【解决方案6】:

        你可以使用这个代码

        var imageView = UIImageView(image: UIImage(name:"imageName"));
        imageView.frame = CGrectMake(x,y imageView.frame.width*0.2,50);
        

        var imageView = UIImageView(frame:CGrectMake(x,y, self.view.frame.size.width *0.2, 50)
        

        【讨论】:

          【解决方案7】:

          imageView 是主视图的子视图。它是这样工作的

          image.frame = CGRectMake(0 , 0, super.view.frame.width, super.view.frame.height * 0.2)
          

          【讨论】:

            猜你喜欢
            • 2017-02-28
            • 2016-06-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-11-02
            • 2015-09-17
            • 2021-04-23
            • 1970-01-01
            相关资源
            最近更新 更多