How can I set programmatically from code the size

2019-09-10 06:38发布

问题:

This is actually a follow up to this question - How can I display image on fullscreen when user taps on my UIImage in Swift?

I managed to add a tap listener to my uiimageview, but now I would like to expand it when user clicks it. I have an iboutlet for that photo:

@IBOutlet weak var requestPhoto: UIImageView!

and then the listener so far prints this string:

func tapHandler(sender: UITapGestureRecognizer) {
    if sender.state == .Ended {
        print("photo tapped")
    }
}

Now instead of printing the string I want to show the photo on the full screen, but so that it could work on any size of the iphone screens - is there a way of doing that directly from the code?

回答1:

Quite easily :

self.requestPhoto.frame = self.view.frame

Bear in mind though, that this will not adjust after rotating the screen. If you support both portrait and landscape it would be best to get outlest for constraints and manipulate them.

Also this method will preserve navigation and tab bars (the photo won't cover them). If you want "full" fullscreen, you should take the frame of UIWindow



回答2:

  1. You can set transparent UIButton and connect to another ViewController
  2. Create new Viewcontroller and set new UIImageView
  3. Go to Storyboard click segue symbol line and go to Right Panel Asisten Inspector
  4. Set Kind: Present Modally - Presentation: Current Context : Animates

Storyboard Segue