How to set full width image inside UIScrollView in swift

I need to display large content in mine UIViewController, added to the Storyboard, so I added UIScrollViewwith restrictions on the top, right, bottom, left: 0 (to make it fullscreen).

At the top, UIScrollViewI need a square image with its width as the screen size of the device, so I added UIImageViewwith restrictions: aspect ratio → 1: 1, top: 0, center alignment in the container, height: 100. And below that there is UILabelwhere I want show large text. I use automatic layout.

  • Is there any way to do this in a storyboard?
  • In quick I tried below

The connected image in the controller file:

@IBOutlet weak var eventThumb: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()
    // set image path from url ,it works
    eventThumb.image = UIImage(data: NSData(contentsOfURL: NSURL(string: self.event!.image)!)!) 

    let screenSize: CGRect = UIScreen.mainScreen().bounds
    eventThumb.frame = CGRectMake( 0,  0,  screenSize.width,  screenSize.width)

}

, , , , .

swift ios, - ?

:

enter image description here

+4
3

:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    // your layout code goes here, AFTER the call to super
}

.

+6

eventThumb.frame = CGRectMake( 0,  0,  screenSize.width,  screenSize.width)

,

eventThumb.frame = CGRectMake( 0,  0,  screenSize.width,  screenSize.height)
+1

:

  • , Image

enter image description here

  • , , Imageview

:

  • I already tried this solution quickly and worked for me. I hope this works for you.

Edited by:

  • As you mentioned, you are using Auto layout, so there is no need to disable it. Just do 1 thing.
  • Put the line that you are setting in frame view in this method: - (Invalid) viewDidLayoutSubviews
0
source

Source: https://habr.com/ru/post/1622792/