View is not refreshed on UI after resize

Hello

I have UIImageView and I am resizing it dynamically according to the Image it contains.

Since the ContentMode is AspectFit, I want to adapt the parent UIImageView, so that the frame is the same as the frame of the Image itself.


This is done of course programatically, the frame is calculated correctly (for test I have put also some dummy values),

the frame is assigned to the UIImageView, but on the UI it stays the same.


newViewFrame = <Calculated frame>

...

self.ImageView.frame = self.newViewFrame!


This is working OK with iOS 12, but with iOS 13 beta is not working.

What is changed here? What else I need to do?

Thanks

Ante

Replies

Tes, there have been some change on some delegate functions call.

h ttps://github.com/ReSwift/ReSwift/issues/412

and

https://developer.apple.com/videos/play/wwdc2019/224/


From which func to you set the new frame ?

Have you checked this func is called ?


Could you show the code ?

Here is a part of a code

I call it in the viewDidAppear.


override func viewDidAppear(_ animated: Bool) {

super.viewDidAppear(animated)

resizeView()


private func resizeView() {

// calculate the new frame for the background image view for border around the image

newBackgroundImageViewFrame = CGRect(x: 20, y: 20, width: 100, height: 150)

}


self.backgroundImageView.frame = newBackgroundImageViewFrame!


I check, the method is executed.

First the image itself need to adapt itself inside of the ImageView (using AspectFit- mode)

Then the ImageView needs to shrink the frame to be the same as the Image- frame.

It works with iOS12, but not in 13.1

Where is resizeView() called ? Only in viewDidAppear ?

If so, why should this be called when resized ?


Try to add after setting the frame

self.backgroundImageView.setNeedsDisplay()


However, there are a few things I do not understand:

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        resizeView()

    private func resizeView() {
        // calculate the new frame for the background image view for border around the image
                       newBackgroundImageViewFrame = CGRect(x: 20, y: 20, width: 100, height: 150)
        }

        self.backgroundImageView.frame = newBackgroundImageViewFrame!


How is newBackgroundImageViewFrame defined ? Why unwrap ?


With such a simple resizeView, could write


    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        // calculate the new frame for the background image view for border around the image
        newBackgroundImageViewFrame = CGRect(x: 20, y: 20, width: 100, height: 150)
      // TO TEST, ADD log
        print(#function, "new frame", newBackgroundImageViewFrame)
        self.backgroundImageView.frame = newBackgroundImageViewFrame!     // Why unwrap ?

Yes, only in viewDidAppear.

If I remember correctly, I had to put it there, because at that time the image is drawn with the correct frame according to the AspectFit- mode.

And then immediately I want to shring the parent ImageView to the same frame.


I tried with setNeedsDisplay(), but unfortunately no change.


The frame- variable was declared outside, therefore unwrapping.

var newBackgroundImageViewFrame:CGRect? = nil


if newBackgroundImageViewFrame == nil {

let imageSize = self.backgroundImage?.size

...

// newBackgroundImageViewFrame = CGRect(x: hOffset, y: vOffset, width: realImageWidth, height: realImageHeight)

newBackgroundImageViewFrame = CGRect(x: 20, y: 20, width: 100, height: 150)

}


But you are right, I can simplify it, at least for testing.

But viewWillAppear will not be called.


Do you connect the imageView to any action ?

Or do you change size only here in viewDidAppear ?


What was the initial view frame ?

Did you define constraints on this imageView ? That should explain there is no change even with needsDisplay.


EDITED.

I moved the code to viewDidLayoutSubviews, and it works OK.


     override func viewDidLayoutSubviews() {
          super.viewDidLayoutSubviews()
          let newBackgroundImageViewFrame = CGRect(x: 20, y: 20, width: 100, height: 150)
          self.self.backgroundImageView.frame.frame = newBackgroundImageViewFrame
     }

I change the size just in viewDidAppear.

Yes, there were constraints on the imageView and probably this is the reason, as you said, why it is not working.

But, what confuses me is that the same code works OK with iOS 12.


btw. I tried this change also with viewDidLayoutSubviews(), but again no luck.


I guess I will have to tackle and change the constraints.

The older code I tested with constraints and change frame in viewDidLoad did not work on IOS 12.


The new one when change frame in viewDidLayoutSubviews does work.


Tested on IOS13 simulator, works when change frame in viewDidLayoutSubviews.


And I did not change any constraint.


Could you post the complete code of the class where this occurs ?

I have created now simple test project with Xcode 10.3 on iOS12 and tried with Simulator.

I have added ImageView on the Main storyboard and set constraints to the Safe Area (Leading, Trailing, Top, Bottom).

And this is the whole code


class ViewController: UIViewController {


@IBOutlet weak var imageView: UIImageView!

override func viewDidAppear(_ animated: Bool) {

super.viewDidAppear(animated)

let newBackFrame = CGRect(x: 20, y: 20, width: 100, height: 150)

self.imageView.frame = newBackFrame

}


And it works, the ImageView is resized when shown.


I tried the same with Xcode 11 beta, iOS13 with Simulator and it does NOT work, the ImageView is not resized.

That proves my first thesis.

Could you please try the same?


It seems that it works with viewDidLayoutSubviews() as you suggested 🙂


Tho problem is with my View that ImageView has parent UIView and the constraints to it (same width/height, centering).

When I did the same in the sample project, it does not work.


I am trying to find a solution.

If you have an idea how to overcome this, that would be great.

Maybe if you have some knowledge about what was changed in this part with iOS13.


Anyway thanks a lot for your support