简体   繁体   中英

UIImageView on top and set size

1) I have a UIView with a UIImageView on it, i load pictures from the image gallery. I want to know how I would "center" the picture on the screen and make that the aspect ratio isn't changed. For instance if I load a picture that is on landscape mode, how do I make sure that the picture isn't stretched all over the screen? My app is working in portrait mode.

2) In that same view I want to add the effect that when the user touches the screen the botton menu fades out and back in when he presses again. I'm not sure what that is called and I couldn't get the tap gesture recognizer to work.

EDIT:

I set the UIImageView on page load

- (void)viewDidLoad
{
[super viewDidLoad];
xlabel = [[UILabel alloc] initWithFrame:self.setLablePosition];
xlabel.backgroundColor = [UIColor clearColor];

imgView.image = self.appDelegate.theImg; // now the image is put on a UIImageView that is "full screen"
xlabel.text = @"Some text";
[imgView addSubview:xlabel];
// Do any additional setup after loading the view.
}

and as for Tap gesture :

-(IBAction)screenTapped{
NSLog(@"screen Tapped");
}

the IBAction is linked to the tap gesture recognizer .

For centering the UIImageView in the view you would do the following

//First give imgView the correct size
//You may need to divide the size by some constant if the image is too big
imgView.frame = CGRectMake(0, 0, self.appDelegate.theImg.size.width, self.appDelegate.theImg.size.width);
//Then center it
imgView.center = self.view.center;

For adding the tap gesture recognizer do the following

UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(screenTapped)];
//By deafualt interaction is disabled
imgView.userInteractionEnabled = YES;
[imgView addGestureRecognizer:recognizer];

My solution was

1) add imgView.contentMode = UIViewContentModeScaleAspectFit;

2) like Omar Abdelhafith said i added imgView.userInteractionEnabled = YES; , the UITapGestureRecognizer for me was created it the storyboard.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM