Resolved problems to put an image at the right spot

Discussion in 'iOS Programming' started by grandM, Aug 18, 2015.

  1. grandM macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #1
    Hi guys

    I'm using auto layout. I put an image inside an UIImageView. This imageView is positioned using auto layout. As long as I am using a natural image all is fine. Of course when no image was provided I want to add a plus sign. This plus sign gets stretched because the imageView is using scaleToFill. So I decided to put this code inside viewDidLayoutSubViews():

    Code:
    self.wifeImageView.frame = CGRectMake(self.wifeImageView.frame.origin.x + self.wifeImageView.frame.height * 2/3, self.wifeImageView.frame.origin.y + self.wifeImageView.frame.height * 1/3 , self.wifeImageView.frame.height * 2/3, self.wifeImageView.frame.height * 2/3)
    
                }
    
    
    
                self.wifeImageView.contentMode = UIViewContentMode.ScaleToFill
    
                self.wifeImageView.clipsToBounds = true
    
                self.wifeImageView.image = UIImage(named: "Plus")
    
    This code works fine. Till I rotate the device. Then it's stretched all over again. If I use the same technique inside didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) the imageView keeps getting smaller and shifting. Anyone got a suggestion how I solve the rotating part? Thanks!
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    If you're using auto layout then you don't set the frame in code. The constraints control the frame.

    I recommend the tutorials on raywenderlich.com to learn auto layout.
     
  3. grandM thread starter macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #3
    Problem was the image got streched too much. I solved it by putting it a frame with a lot of whitespace. Once again my sincere thanks.
     

Share This Page