4

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

Community
  • 1
  • 1
Dashrath
  • 2,141
  • 1
  • 28
  • 33
  • eventThumb.frame = CGRectMake( 0, 0, screenSize.width, screenSize.width). this code is not making the image full width same as screen size and I am getting same size image displayed in storyboard – Dashrath Jan 05 '16 at 04:48

3 Answers3

6

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.

nurnachman
  • 4,468
  • 2
  • 37
  • 40
Darko
  • 9,655
  • 9
  • 36
  • 48
  • great answer! small typo: finish the function signature with "()" before the "{" and the func should be "override" +1 – nurnachman Dec 26 '16 at 10:49
1

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)
Caleb Kleveter
  • 11,170
  • 8
  • 62
  • 92
0

Simply Try this Steps:

  • First Remove all red lines here you can see in Image

enter image description here

  • Now keep that line as it is, where you are setting a frame of Imageview

Note:

  • I already tried this solution in swift & worked for me. I hope it will work for you.

Edited:

  • As you mentioned you are using Auto layout, So no need to disable it. Just do 1 thing
  • Put the line which you are setting up the frame of image view in this method: -(void)viewDidLayoutSubviews
Mayur Prajapati
  • 5,454
  • 7
  • 41
  • 70
  • Thanks , But I am using Auto Layout in my app.and your solution seems to be using fixed width layout. – Dashrath Jan 05 '16 at 05:05
  • my concern is, since I am using Auto Layout in whole application, I am not getting the option you suggested.If I remove Auto Layout option from storyboard is will affect whole app which I don't want to use only for this option. Can I still do it with Auto Layout option selected ? – Dashrath Jan 05 '16 at 05:14
  • since I am using Auto Layout in whole application, I am not getting the option you suggested. I have added image in my question edit of what I am getting now. – Dashrath Jan 05 '16 at 05:31
  • I added override func viewDidLayoutSubviews(){ let screenSize: CGRect = UIScreen.mainScreen().bounds eventThumn.frame = CGRectMake(0,0, screenSize.height , screenSize.width) } it set the image size on screen load. but when I scroll page ( as it is in scroll view ) it resets to old size – Dashrath Jan 05 '16 at 06:05