user6064880
user6064880

Reputation:

Update ImageView frame size for Larger screens

I want to update frame width and height of my ImageView if its an iPad.I have tried doing something like this, its working but the view getting updated only if any UI interaction is performed.

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil isKneeMap:(BOOL)tIsKnee{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
            // Custom initialization            
    } if(I_PAD){
        VAL = IS_IPAD ? 1.5 :1;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.ImgViewL = [[UIImageView alloc]
                     initWithFrame:CGRectMake(0,0,140*VAL,290*VAL)];
    self.view = self.myView;
}

I also tried [self.view LayoutIfNeeded] but no changes same result.Is there any better way of doing this.Please help me out of this issue.

Upvotes: 1

Views: 86

Answers (2)

Ashley Mills
Ashley Mills

Reputation: 53121

As mentioned in the comments, you should never set the frame directly of views that use auto layout.

Your method of determining the image view size has potential problems - you're using the same size on an SE and Plus sized devices, and on the 9.7" iPad the 12.9" iPad pro. You should consider setting up your constrains to use proportional sizing…

enter image description here

In the example above, the image view will have a width of 140pt on an iPhone 8/X, 124pt on the SE, and 155 on Plus size devices.

Alternatively, if you want to achieve your 1.5 iPad multiplier using the storyboard, you can add different constants for variations on width & height…

enter image description here

This will give widths of 140 on all iPhones in portrait orientation (compact width, regular height) and 210 on iPads in all orientations (regular width & height)

Upvotes: 1

Seref Bulbul
Seref Bulbul

Reputation: 1173

Use outlets of the width and height constraints, update them and call layoutIfNeeded().

@IBOutlet weak var imageViewWidthConstraint: NSLayoutConstraint!
@IBOutlet weak var imageViewHeightConstraint: NSLayoutConstraint!

imageViewWidthConstraint.constant = 140*VAL
imageViewHeightConstraint.constant = 290*VAL

view.layoutIfNeeded()

Upvotes: 1

Related Questions