Make UIView fill UIScrollview - ios

I have a paging scrollview with two views, and the first one is to fill the UIScrollview and the second to stay in the middle. I don't know what's going wrong, but it wouldn't fill on iPhone6 but, it fits fine on iPhone5. Below is my code:
View Hierarchy
UIView
--StackView (Properties -- Axis(Vertical) Alignment(Fill) Distribution(Fill) Spacing(0))
--UIView (Fixed height. 60)
--UIView (Main Views Holder)
--ScrollView(Content Size: Screen Width * 2, Height: Height left between it margin to the MainView and BottomView) and I notice, that the scrollview keeps remaining size 433 on both iPhone5 and 6. And when I check my storyboard, that's the exact same height there
--UIView(Height: 102, Width: Screen Width)BottomView
#IBOutlet weak var scrollView: UIScrollView!
var rectanglePhot: UIView!
var squarePhoto: UIView!
override func viewDidLoad() {
super.viewDidLoad()
scrollView.contentSize = CGSize(width: self.view.frame.size.width * 2, height: scrollView.frame.size.height)
scrollView.autoresizingMask = .FlexibleWidth
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.backgroundColor = UIColor.greenColor()
iniiCameraView()
}
func iniiCameraView() {
rectanglePhot = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.scrollView.frame.size.height)) <-- This is the problematic one
//rectanglePhot = UIView()
let specifyY = self.scrollView.frame.size.height - self.view.frame.size.width
squarePhoto = UIView(frame: CGRect(x: self.view.frame.size.width, y: specifyY, width: self.view.frame.size.width, height: self.view.frame.size.width))
scrollView.addSubview(rectanglePhot)
scrollView.addSubview(squarePhoto)
rectanglePhot.backgroundColor = UIColor.blueColor()
squarePhoto.backgroundColor = UIColor.brownColor()
print("Rect Height ", rectanglePhot.frame.size.height)
print("Scroll Height ", scrollView.frame.size.height)
print("Specify Y ", specifyY)
}
And the image Image Added to Scrollview a index[0]

Add the code in viewdidappear. The autolayout will be applied after viewdidload

Replace
UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.scrollView.frame.size.height))
With :
UIView(frame: CGRect(x: 0, y: 0, width: self.scrollView.bounds.size.width, height: self.scrollView.bounds.size.height))
With this the UIView will take the exact size of you scrollview

if you don't want to use autolayout: add subviews in viewDidLoad, that is fine but you also need to adjust their frames in viewDidLayoutSubviews method of UIViewController since your scrollView will have a different frame after autolayout passes.
if you are ok with using autolayout: don't rely on scrollView for sizing your subviews, relate size of your subviews to the size of self.view

Related

Xcode scrollview not working

I'm making a game and need users to sign an End User License Agreement (EULA). The EULA is long and requires a scroll view. I want the screen itself to stay still, so the buttons and header are always present:
Here is a screenshot of my viewController storyboard:
here's my code:
#IBOutlet var eulaView: UIView!
#IBOutlet var scrollView: UIScrollView!
#IBOutlet var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let width = eulaView.frame.width
let height = eulaView.frame.height
self.scrollView.frame = CGRect(x: 0, y: 0, width: width, height: height)
self.label.frame = CGRect(x: 0, y: 0, width: width , height: 2000)
self.label.text = "Lots of stuff"
}
Strangely, if I set self.label.frame's width to 100, it shows up. If I have it at 300, or "width", the label disappears.
No matter what, the scroll view doesn't scroll.
PS: I had it working earlier, but then I tried it on an iPhone X simulator and it was blank.
You need to set self.scrollView.contentSize = CGSize(width: width,height: 2000)
Sometimes When you change device in storyboard at that time screen was blank may be its bug of xcode.
To find the Dynamic Height Of label Depends on String Of label.
func heightForView(text:String, font:UIFont, width:CGFloat) -> CGFloat{
let label:UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: width, height: CGFloat.greatestFiniteMagnitude))
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.byWordWrapping
label.font = font
label.text = text
label.sizeToFit()
return label.frame.height
}

swift 8 iOS data is getting crop in scrollview while adding dynamically data

Hi I am developing small IOS application in which I am using scrollview with auto-layout.Inside scroll I am adding two more views. I am using IB and auto-layout constraints. I am adding two views in side one after another in vertical manner. I have added outer constraints like trailing, leading, top, bottom space. I also added height constraints for both views. Till this everything is working fine.
But my passengerView has some dynamic content. For that reason I want to make height constraint greater than equal to instead of equal to.
my code is:-
#IBOutlet weak var secondView: UIView!
#IBOutlet weak var scrollView: UIScrollView!
#IBOutlet weak var passengerView: UIView!
#IBOutlet weak var detailView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let nameLabel = UILabel(frame: CGRect(x: 0, y: (self.name.frame.height * CGFloat(index) + CGFloat(49)), width: 161, height: 32))
let idLabel = UILabel(frame: CGRect(x: 161, y: (self.name.frame.height * CGFloat(index) + CGFloat(49)), width: 161, height: 32))
// nameLabel.text = dsdh?["name"] as? String
nameLabel.layer.borderWidth = 1
nameLabel.layer.borderColor = UIColor.black.cgColor
nameLabel.textAlignment = .center;
self.passengerView.addSubview(nameLabel)
//idLabel.text = dsdh?["document_Type"] as? String
idLabel.layer.borderWidth = 1
idLabel.layer.borderColor = UIColor.black.cgColor
idLabel.textAlignment = .center;
self.passengerView.addSubview(idLabel)
self.secondView.addSubview(self.passengerView)
self.detailView.frame = CGRect(x: self.detailView.frame.origin.x, y: self.detailView.frame.origin.y + CGFloat(32) , width: self.detailView.frame.size.width, height: self.detailView.frame.size.height)
self.secondView.addSubview(self.detailView)
//self.scrollView.addSubview(self.detailView)
self.secondView.frame = CGRect(x: self.secondView.frame.origin.x, y: self.secondView.frame.origin.y , width: self.secondView.frame.size.width, height: self.secondView.frame.size.height + CGFloat(5))
self.scrollView.addSubview(self.secondView)
//self.scrollView.contentSize = CGSize(width: self.scrollView.frame.size.width , height: self.scrollView.frame.size.height + CGFloat(32))
self.scrollView.frame = CGRect(x: self.scrollView.frame.origin.x, y: self.scrollView.frame.origin.y , width: self.scrollView.frame.size.width, height: self.scrollView.frame.size.height + CGFloat(0))
self.view.frame = CGRect(x: self.view.frame.origin.x, y: self.view.frame.origin.y , width: self.view.frame.size.width, height: self.view.frame.size.height + CGFloat(0))
}
The best way to do this would be as following:
Implement a UIScrollView that is constraint to the Leading, Trailing, Top, Bottom constraints of your main view.
Add a UIView to the UIScrollView and constrain it to the Leading, Trailing, Top, Bottom of your UIScrollView. Important is to also constrain this UIView's width to your main view, not your scroll view! In this way this UIView will serve you as a Content View for the UIScrollView. This step is mandatory if you are doing this in Interface Builder!
Add a UIStackView to your Content View (the view from the previous step) and constrain it to the Leading, Trailing, Top, Bottom of the Content View.
Add all your UILabels, your Detail View, QR Code View and Passenger View as children to this UIStackView.
Since you want only your Passenger View to have a dynamic height you can set your UIStackView's axis to Vertical and Content Mode to Fill. Then you will provide a height for all of the children of the UIStackView except the Passenger View.
In this way the Content Size of your UIScrollView will adapt based on the height of the UIStackView and the height of the UIStackView willl adapt based on the needed height for the Passenger View.

The bottom content of the scrollView is not not responding to touch events

I have scroll view. In side scroll view I have a container view with trailing,leading,top and bottom constraints and also equal heights. I'm adding views dynamically. I'd updated the content size in
viewWillLayoutSubviews as follows.
override func viewWillLayoutSubviews() {
self.scrollView.contentSize = CGSize(width: self.contentView.frame.width, height: self.contentView.frame.height)
self.contentView.frame = CGRect(x: self.contentView.frame.minX, y: self.contentView.frame.minY, width: self.contentView.frame.width, height: 2500) // code to update the contentView's frame where 2500 is a dummy value
self.contentView.clipsToBounds = true
}
Now I can scroll until my last subview of the container view. But the problem is the last 3 subviews are not responding to the touch events.
I tried the following ways but failed.
also updated the containterView's frame
also updated the scrollview's frame
did not work.
Please help me.
func updateFrame() {
self.scrollView.contentSize = CGSize(width: self.contentView.frame.width, height: self.contentView.frame.height)
self.contentView.translatesAutoresizingMaskIntoConstraints = false
self.contentView.frame = CGRect(x: self.contentView.frame.minX, y: self.contentView.frame.minY, width: self.contentView.frame.width, height: 2500) // code to update the contentView's frame where 2500 is a dummy value
self.contentView.clipsToBounds = true
}
try this.
I am assuming that your scrollview is bigger then Content view

UIScrollView didn't scroll

I've some problem with ScrollView.
I'm trying to create a scrollview and add to it dynamically some buttons.
So i create a scrollView in my main storyboard with some constraints. (0 to left, 0 to right, 0 to botton and 1/10 height).
Now, i want to add some button.
#IBOutlet var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
for index in 0..<12 {
let frame1 = CGRect(x: 0 + (index * 60), y: 0, width: 45, height: 45 )
let button = UIButton(frame: frame1)
button.setTitle("toto", forState: .Normal)
button.backgroundColor = UIColor.green()
scrollView.addSubview(button)
}
}
So now the problem is : my buttons are present but i can't scroll. The scroll is enabled in the storyBoard. I tried to enabled it in the code but nothing changed..
set the contentSize of your scrollView
self. scrollView.contentSize = CGSizeMake(required_width, required_height)
for example
self. scrollView.contentSize = CGSizeMake(500, 74) // custtomize ur self
You have to set the contentSize of the scrollView
The height to set is probably the origin.y + the size.height of the last button.
use this code to set scrollview height
scrollview.contentInset = UIEdgeInsetsMake(0, 0, 50, 0)
//50 is the height of scroll view you can change it to the height you want
scrollview.bounces = false
or you can also change content inset from here
by changing the values of content inset (height or width)

How to embed stack view in scroll view programmatically

I have tried embedding it, but my stack view is dynamic and my app is also changing orientations time to time. I have segment control at the end of the view.
I have also tried googling it but had no luck. thanks in advance.
So far I have done:
In view did load:
mainStackView.axis = UILayoutConstraintAxis.Vertical
mainStackView.spacing = 3
scrollView.frame = self.view.bounds
scrollView.addSubview(mainStackView)
view.addSubview(scrollView)
In view did layout:
override func viewDidLayoutSubviews()
{
super.viewDidLayoutSubviews()
let top = topLayoutGuide.length
let bottom = bottomLayoutGuide.length
self.mainStackView.frame = CGRect(x: 0, y: top, width: view.frame.width, height: view.frame.height - top - bottom).insetBy(dx: 10, dy: 10)
dispatch_async(dispatch_get_main_queue())
{
self.scrollView.frame = self.view.bounds
self.scrollView.contentSize = CGSize(width: self.view.bounds.width, height: self.segmentedControl.frame.origin.y + self.segmentedControl.frame.height + 50)
}
print(scrollView.contentSize)
}
You need to set the height constraint of segment control.
For Example:
segmentedControl.heightAnchor.constraintEqualToConstant(50).active = true
More over, you can Add Empty bottom view to avoid stack view's must fill mechanism. This will show you desired view output.
var bottomView = UIView(frame: CGRectZero)
stackView.addArrangedSubview(bottomView)

Resources