How to determine when UIView is resized in fast ios xcode?

I am trying to do some things with CATiledLayer inside a UIScrollView.

Somehow, the size of the UIView inside the UIScrollView changes to a large number. I need to find out what exactly causes the resizing.

Is there a way to determine when the size of the UIView (either frame, border) or contentSize of the UIScrollView will be resized?

I tried

override var frame: CGRect { didSet { println("frame changed"); } } 

inside a subclass of UIView,

but it is called only once when the application starts, although the size of the UIView is subsequently changed.

+7
ios cocoa-touch swift uiview uiscrollview
source share
5 answers

viewWillLayoutSubviews() and viewDidLayoutSubviews() will be called whenever the borders change. In the view controller.

+20
source share

Here is the answer:

fooobar.com/questions/44412 / ...

Just paste this outside the method body:

 override var bounds: CGRect { didSet { // Do stuff here } } 
+12
source share

You can also use KVO:

You can set KVO as follows, where view is the view you want to watch for frame changes:

 self.addObserver(view, forKeyPath: "center", options: NSKeyValueObservingOptions.New, context: nil) 

And you can get changes with this notification:

 override func observeValueForKeyPath(keyPath: String!, ofObject object: AnyObject!, change: NSDictionary!, context: CMutableVoidPointer) { } 

observeValueForKeyPath will be called whenever the frame of the view you are observing changes.

Also remember to remove the observer when your view is released:

 view.removeObserver(self, forKeyPath:@"center") 
+3
source share

The answers are correct, although for my case the restrictions that I set in the storyboard caused the UIView to resize without overriding any detection functions.

+2
source share

STEP 1: viewWillLayoutSubviews

Called to notify the view controller that its view is the layout of its subviews

When species change is limited, presentation adjusts the position of its subspecies. Your view controller can override this method to make changes before the view outlines its subclauses. By default, the implementation of this method does nothing.

STEP 2: viewDidLayoutSubviews

Called to notify the view controller that its presentation has just been laid out in its subclauses.

When the borders are changed to represent the view controller, the view adjusts the position of its subzones, and then the system calls this method. However, this method, which is called, does not indicate that individual layouts of view representations have been adjusted. each subview is responsible for setting its own layout.

Your view controller can override this method to make changes after the view outlines its approaches. By default, this method does nothing.

Above these methods is called whenever the borders of the UIView are changed .

+1
source share

All Articles