My code with scrollview is not working? - ios

I am using swift to get my scrollview going the structure of my elements is as shown
The code that i am using in my view controller is
import UIKit
class scrollview: UIViewController {
#IBOutlet weak var scroller: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
scroller.userInteractionEnabled=true
scroller.contentSize=CGSizeMake(scroller.frame.size.width, scroller.frame.size.height)
But i am not able to make my page scroll can anyone help me out with this am i missing any constrains or something wrong with my code?

scroller.contentSize=CGSizeMake(should be total width of the labels not scroll view width, should be total height of the labels not scrollview height)
contentsize should be greater than frame size for scrolling.

In order for UIScrollView to scroll, the content size must be greater than its frame size. If, for example, you want to scroll vertically, it has to be true that scrollView.contentSize.height > scrollView.frame.size.height.

Related

how to make scrollable label using scroll view?

I want to make something like article to read view controller that contains UIImage and Label nested in the scroll view.
I want if the article content is not to big, the scrolling function is disabled, but if the content is big, I want it can be scrolled and it can be scrolled as the same height of the label content height.
(if the content article is not too much, then i don't need to scroll)
but if the content is a lot then I expect that I can scroll. at the moment, I can scroll if the content is big like below
I have tried to set the bottom anchor like my code below but it doesn't work. here is the autolayout I use for the bottom label contraint andthe view hierarchy
class NotificationDetailVC: UIViewController {
#IBOutlet weak var notificationTitleLabel: UILabel!
#IBOutlet weak var notificationImage: UIImageView!
#IBOutlet weak var notificationDateLabel: UILabel!
#IBOutlet weak var notificationScrollView: UIScrollView!
#IBOutlet weak var notificationContentLabel: UILabel!
var dataNotification : [String:Any]?
override func viewDidLoad() {
super.viewDidLoad()
notificationScrollView.contentLayoutGuide.bottomAnchor.constraint(equalTo: notificationContentLabel.bottomAnchor).isActive = true
}
}
Don't make bottom constraint to greater than. just set it equal to.
Scroll view has a content view which contains all scrollable view. You have to set height, width and x,y position of content view.
Set top, bottom, leading and trailing constraint to content view.
Scrollview's scrolling directions depands on height and width of contantview. if you set content view's width equal to scrollview than scrollview is not scrollable in horizontal direction. Same for height , if you set content view's height equal to scrollview's height than scrollview is not scrollable in vertical direction.
In your case you have to set content view's width equal to scrollview's width therefore scrollview is not scrollable in horizontal direction. Content view automatically get height according to subviews(In your case UIimageview and uilabel). so set height, top, bottom constraint of imageview and top and bottom constraints of uilabel because uilabel height automatically set by it's text. Set number of line = 0 in UILabel.
Set Label bottom constaint equal to any constant height (say 20) & number of lines = 0.
as you are using scrollview then you have to give height to the imageview. You don't need to worry about label height bcz its heigght will be calculated by its content height.
Now your scrollviewContentHeight = imgView.height + all labels content height. Make sure each component (imageview & every label) have top and bottom constraint.
That's all you have to do.

UIStackview fill proportionally in a UITableviewCell takes incorrect frame

Adding proportional fill setting in a UIStackview won't let the view stretch properly. Is this the expected behaviour?
Fill proportionally' distribution type works with intrinsic content size.
So if our vertical stack(height say 600) view has 2 views, ViewA (intrinsic content height 200) and ViewB(intrinsic content height 100), the stack view will size them to ViewA(height 400) and ViewB(height 200).
Here in IB what you see is not what you get.
Dragging to make frames change is useless. Just run the app.
You will see the expected behaviour only when the child views somehow get the intrinsic/constrained height.
How it looks in IB Here the top stack view has views constrained to be of height minimum 10 and 30, i.e. ration 1:4.
What we really get
Top stack view is what we had expected to look like. View with height in ratio 1:4. And bottom one with ratio 1:1, not expected.
You can fix this by creating a custom view
class CustomHeightView: UIView {
var height = 1.0
override public var intrinsicContentSize: CGSize {
return CGSize(width: 0.0, height: height)
}
}
Change the class of your UIView to CustomHeightView
So in the Controller create an outlet for the UIViews
#IBOutlet weak var header_one: CustomHeightView!
#IBOutlet weak var header_two: CustomHeightView!
Then in your viewDidLoad set the proportion the way you want it
override func viewDidLoad() {
super.viewDidLoad()
header_one.height = 1.8
header_two.height = 1.2
}

UIScrollView Cutting Off Bottom Content

I'm having an extremely sad time trying to get my scrollView to display content correctly.
For one page it seems to show a heap of blank content at the bottom and for another it cuts off before reaching the bottom... extremely sad. I've set the constraints of the scrollView to 0 for each side.
Xcode screenshot
Screenshot of all the space
I've been playing around with trying to set the contentSize but that doesn't seem to do anything.
#IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
self.automaticallyAdjustsScrollViewInsets = false
self.scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height)
print("Width: \(self.view.bounds.size.width) Height: \(self.view.bounds.size.height) scrollView: \(self.scrollView.contentSize)")
// prints -> Width: 375.0 Height: 667.0 scrollView: (375.0, 667.0)
}
Hard to tell what it may be with only the screenshot, but I have some suggestions. Select the ScrollView, then the ruler and:
Check if the indicator insets are all zeros.
In arrange select fill container horizontally and fill container vertically.
Try to change the height value of the height to check if it is set bigger than it needs to be.
Below a screenshot of what you should see

UIScroll View Not Working As Expected

I have a scene where I just want the scroll to be vertical not horizontal. I have tried to program it to disable but that doesn't seem to work. I also run into the issue where it isn't auto resizing to different phone screens. Im kinda new at this and I'm just really stuck. Any help?
Disable Code:
#IBOutlet var scrollview: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
scrollview.contentSize.height = 800
scrollview.contentSize.width = 0
I think problem is with this line
scrollview.contentSize.width=0
Try to set width equal to screen width like this,
scrollview.contentSize.width=[UIScreen mainScreen].bounds.size.width;
Scrollview will scroll horizontally when it's contentSize.width is greater than viewcontroller's width.
Hope it will solve the issue.
I think you are not applying constraints on UIScrollView properly now first of all you have to properly add constraint on scrollview and no need to set contentsize from code.
http://spin.atomicobject.com/2014/03/05/uiscrollview-autolayout-ios/
Hope this tutorial will help you better understand the ScrollView
var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
self.scrollView = UIScrollView()
self.scrollView.contentSize = CGSizeMake(1000, 1000)
self.view.addSubview(scrollView)
}

Get number of column in a CollectionView IOS

I use an UICollectionView in my first app. My problem is I can't get the number of column (or line) calculated in the UICollectionView.
The UICollectionView is inside an UIScrollView, and I want to set the height of the UICollectionView because I don't want to scroll in the UICollectionView but I want to see all the items.
Is this possible?
Thanks,
F.G
After many researches I found the solution:
I don't need to know the number of column or line in the collection view if I want to fix the height.
The height of the collection view can be updated automatically:
Add a height constrain to the collection view
Add the CollectionView and the constrain as outlets in the ViewController:
#IBOutlet weak var heightConstrainCollectionView: NSLayoutConstraint!
#IBOutlet weak var myCollectionView: UICollectionView!
After the view Load, update the height of the CollectionView:
override func viewDidAppear(animated: Bool) {
super.viewDidLoad()
heightConstrainCollectionView.constant = myCollectionView.contentSize.height
}
If the user rotate the device, update the CollectionView's height:
override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
heightConstrainCollectionView.constant = myCollectionView.contentSize.height
// Add this if you want to see the height after rotation
println("New Width: \(newspaperCollectionView.contentSize.height)")
}
Now I can't scroll in my CollectionView and the CollectionView show all the items

Resources