Swift - After loading subview from xib to UIScrollView, scroll is not working and subview is going out of bounds
Clash Royale CLAN TAG#URR8PPP
Swift - After loading subview from xib to UIScrollView, scroll is not working and subview is going out of bounds
I have the following scenario:
View1.xib UIView
Im having a coco touch class of type UIView for loading the Nib named View1. Also I have made it as owner of the previous xib. Below is the code I'm using to load the view:
override init(frame: CGRect)
super.init(frame: frame)
commitInit()
required init?(coder aDecoder: NSCoder)
super.init(coder: aDecoder)
commitInit()
private func commitInit()
Bundle.main.loadNibNamed("View1", owner: self, options: nil)
containerView.frame = self.bounds
containerView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
addSubview(containerView)
Now in the storyboard I included the scrollview and a sub UIView under it. I have defined the Subview type as View1.
ScrollView in main storyboard
Scrollview constraints
ScrollView Properties
Emulator
Now when I'm running the project I'm getting the screen but I'm unable to scroll through the view included. Due to this some elements on top and bottom becomes hidden (Out of bounds) and I'm unable to scroll as well. Am I doing anything wrong? Please help me out.
Note - I'm very new to swift, any help is greatly appreciated. Thanks in advance.
2 Answers
2
I didn't understand some points: Point 1. So i should declare containerView.translatesAutoresizingMaskIntoConstraints = false in the UIView class? Point 2: For the UIView under scroll view I have already made it to be equal width and height as superview (UIScrollView). What exactly you mean by setting dynamic height of the scrollview outlet? Please do let me know. Thanks
– Abhishek Paul
Aug 8 at 3:55
Your newView
can't flow out of scrollView
's bound, Because it has same height with your scrollView
. But your newView
's content flows out of newView
's bounds.(You can see if you put your newView
out of scrollView with same height and set it a backgroundColor
or set clipToBounds
property true
) That's why you can't scroll.
newView
scrollView
scrollView
newView
newView
newView
backgroundColor
clipToBounds
true
To Sove This, you should find another source for your newView
's height
(Maybe a constant value or its subview's heihgt or safeArea.heihgt
. This is your choice)
newView
height
safeArea.heihgt
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Please don't shout. It doesn't make a difference to the people you are asking the question to. It may even make people not approach it even.
– Rakesha Shastri
Aug 7 at 19:28