Search code examples
objective-cios7xcode5

Constrains on imageView doesn't work


I'd like to optimize my app for the 3,5 inch display. The imageView is at the bottom of the view at the 4 inch display. But if I add constrains on my imageView it's not rescaled and I don't see the whole imageView at the 3,5 inch display.

-(void)viewDidAppear:(BOOL)animated {

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
    if ([[UIScreen mainScreen] scale] == 2.0) {
        if([UIScreen mainScreen].bounds.size.height == 568){
            // iPhone retina-4 inch
        } else{

            [self.scrollViewImgView setFrame:CGRectMake(0, 213, 320, 267)];
        }
    }
}

}


Solution

  • The easiest way to do this is using the layout constraints within the interface builder.

    Using auto-layout on storyboard or xib you need to add at least 4 constraints per object. I've added a picture of how to add constraints to a UIImageView object which is close to the bottom of the view

    enter image description here

    Select the UIImageVIew object on the view then press the constraints icon like below. Within this constraint options view here I've added a constraint fixing the distance to the object to its left (in this case is the side of the view) and a constraint fixing to the bottom of the view. Highlighting the spring icons (turning orange colour) means they have been selected. I also have the height and width of the UIImageView object constraints selected here as I also want these properties not to change (get squashed).

    Here is a link to an auto-layout tutorial.

    beginning-auto-layout-tutorial-in-ios-7-part-1

    I hope this helps.