问题
I am working on a Universal Application. I have a View-based application which has one view controller. I am adding a UIImageView
as a subview.
My problem is that if I put the ImageView in viewDidLoad
, it is getting resized in iPad.
But if I add the ImageView to view controller dynamically on button tap event, the UIImageView
is not getting resized as per iPad. Instead, it is showing ImageView with 320 x 480 dimensions.
Note: My View Controller has setAutoresizesSubviews
to YES
.
My code is as below:
-(void)buttonTap
{
UIImage *img = [[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"main-bg" ofType:@"png"]];
UIImageView *imgViewMainBG = [[UIImageView alloc]initWithImage:img];
imgViewMainBG.frame = CGRectMake(0,0,320,480);
imgViewMainBG.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:imgViewMainBG];
[imgViewMainBG release];
[img release];
img=nil;
}
回答1:
AutoresizingMask is for changing the size of all subviews according to the change in size of the superView whenever the superView's size is changed, it won't resize subviews at the time of adding a subView.
But you can find the bounds or frame of self.view and set the frame property of imageView according to that bounds like below
[imgViewMainBG setFrame:CGRectMake(0, 0, self.view.frame.size.width,
self.view.frame.size.height)];
so that imgViewMainBG is fully visible in ipad
回答2:
Try changing image view's contentMode
property to UIViewContentModeScaleToFill
.
来源:https://stackoverflow.com/questions/12248296/uiview-subview-not-resizing