Programmatically change the height and width of a UIImageView Xcode Swift

前端 未结 7 833
一整个雨季
一整个雨季 2021-02-03 23:00

Hey for some reason I am struggling with trying to set the height and width of one of my image views. I want to set it so the height only goes for 20% of my screen. I know to re

相关标签:
7条回答
  • 2021-02-03 23:19

    imageView is a subview of the main view. It works this way

    image.frame = CGRectMake(0 , 0, super.view.frame.width, super.view.frame.height * 0.2)
    
    0 讨论(0)
  • 2021-02-03 23:21
    let screenSize: CGRect = UIScreen.mainScreen().bounds
    image.frame = CGRectMake(0,0, screenSize.height * 0.2, 50)
    
    0 讨论(0)
  • 2021-02-03 23:25

    A faster / alternative way to change the height and/or width of a UIView is by setting its width/height through frame.size:

    let neededHeight = UIScreen.main.bounds.height * 0.2
    yourView.frame.size.height = neededHeight
    
    0 讨论(0)
  • 2021-02-03 23:34

    u can use this code

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

    or

    var imageView = UIImageView(frame:CGrectMake(x,y, self.view.frame.size.width *0.2, 50)
    
    0 讨论(0)
  • 2021-02-03 23:40

    The accepted answer in Swift 3:

    let screenSize: CGRect = UIScreen.main.bounds
    image.frame = CGRect(x: 0, y: 0, width: 50, height: screenSize.height * 0.2)
    
    0 讨论(0)
  • 2021-02-03 23:42

    Using autoview

    image.heightAnchor.constraint(equalToConstant: CGFloat(8)).isActive = true
    
    0 讨论(0)
提交回复
热议问题