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

1
  • 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 Commented Jan 5, 2016 at 4:48

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

great answer! small typo: finish the function signature with "()" before the "{" and the func should be "override" +1
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)

Comments

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

4 Comments

Thanks , But I am using Auto Layout in my app.and your solution seems to be using fixed width layout.
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 ?
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.
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.