Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set full width image inside UIScrollView in swift

I need to display large content in my UIViewController added in Storyboard so I added UIScrollView with constraints top, right, bottom, left:0 ( to make it full screen ).

In top of UIScrollView I need a square image with its width as device screen size, so I added UIImageView with constraints : aspect ratio->1:1, top:0, centre align in container, height : 100. And below It there is a UILabel where I want to show large text. I am using Auto Layout.

  1. Is there an option to do this in Storyboard ?
  2. In swift I tried below

Connected Image in controller file as :

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

}

I tried related answers given here ,here and here but they not seem to working in my case.

I am new in swift and ios, am I doing something wrong in structure ?

Edit : Image added

enter image description here

like image 833
Dashrath Avatar asked Mar 13 '23 17:03

Dashrath


2 Answers

Call it in:

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

You forgot to call super before your code.

like image 176
Darko Avatar answered Mar 24 '23 06:03

Darko


You had tried to make the frame with following screen size

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

Please crosscheck it with

eventThumb.frame = CGRectMake( 0,  0,  screenSize.width,  screenSize.height)
like image 23
Ravikant Bagoria Avatar answered Mar 24 '23 06:03

Ravikant Bagoria