You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Imagine the FrameLayout you just created as a picture frame containing various photos. If you move the picture frame or change its size, the photos inside will also move and their contents will adjust accordingly. Therefore, you just need to set the size of the picture frame to match the size you want to display.
In this case, just set like this:
// assume that frameLayout was placed inside another UIView
overrideopenfunclayoutSubviews(){super.layoutSubviews()frameLayout.frame=bounds}
// or if frameLayout was placed inside an UIViewController
overridefuncviewDidLayoutSubviews(){super.viewDidLayoutSubviews()frameLayout.frame=view.bounds}
@kennic what about to add functionality which allow to avoid that? I also know that sometimes layoutSubviews doesn't work properly and drawRect becomes the only way to get a valid frame
Actually, you can use an autoresizing mask for frameLayout to fit its frame to its superview. Please try that in your code. If it's not working, feel free to share your code here for further assistance.
Also, note that layoutSubviews will always be called after using setNeedsLayout().
Activity
kennic commentedon Nov 14, 2023
Imagine the FrameLayout you just created as a picture frame containing various photos. If you move the picture frame or change its size, the photos inside will also move and their contents will adjust accordingly. Therefore, you just need to set the size of the picture frame to match the size you want to display.
In this case, just set like this:
Gargo commentedon Nov 20, 2023
@kennic what about to add functionality which allow to avoid that? I also know that sometimes
layoutSubviewsdoesn't work properly anddrawRectbecomes the only way to get a valid framekennic commentedon Nov 20, 2023
Actually, you can use an autoresizing mask for frameLayout to fit its frame to its superview. Please try that in your code. If it's not working, feel free to share your code here for further assistance.
Also, note that
layoutSubviewswill always be called after usingsetNeedsLayout().