I put a UIScrollView occupies the whole area of my controller's view. Then I added a UIView (yellow color) on UIScrollView:
In my controller code, I have set the height of my scroll view:
override func viewDidLoad() {
super.viewDidLoad()
self.scrollView.contentSize.height = 1000
}
When I run it and scroll up on the screen, I see this:
How to make the yellow view component's bottom matches the bottom of scroll view?
From the information provided the only guess could be that you forgot to add layout constraints.
In case you did add them, please update the question showing your constraints.
Related
I am trying to add a UIView on top of the messagesCollectionView, but the collectionview seems to take up the whole screen making the UIView I want to add unable to be seen. I am looking at their documentation here: https://github.com/MessageKit/MessageKit/blob/master/Sources/Layout/MessagesCollectionViewFlowLayout.swift
My thoughts are to change something in the Layout delegate but I am unsure what and where to change... any thoughts on this would be appreciated!
Thanks!
If you want to have some sort of floating view above the messagesCollectionView, then you can just add it as a subview to the MessagesViewControler's view, just make sure to do it after you've called super.viewDidLoad() because that's where MessageKit adds the collectionView as a subview so if you add it before then, then your view will be behind the collectionView and won't appear. To prevent the cells overlapping with your view, you can use messagesCollectionView.contentInset property to add padding to either the top or bottom if your view is floating there, so that the user can still scroll to all of the messages. Something like this:
override func viewDidLoad() {
super.viewDidLoad()
let subview = UIView()
view.addSubview(subview)
// Either add constraints or set the frame manually
// ...
// Set the contentInset if you want to prevent the messages from overlapping your view
messagesCollectionView.contentInset.top = 10 // For example, if your view was stickied to the top and was height 10px
}
Another route you could go is to have a parent view controller where you add the MessagesViewController as a child VC to the parent, and then size and layout the messagesCollectionView how you want. You can see an example of this in the MessageKit example app, in the MessageContainerController class: https://github.com/MessageKit/MessageKit/blob/master/Example/Sources/View%20Controllers/MessageContainerController.swift
I have a collection view and a text field on a ui view.
Initially, I have a text field at the top of the ui view and then I set the collection view top Anchor constraint to the bottom of the text field.
I want to implement a behavior where if I scroll down the collection view, the text field should disappear and the collection view's top should be at the top of the same ui view (this would hide the text field). When I scroll up, I want the constraints to be like the initial ones (container view's top Anchor should be set to the bottom of the text field). Any hint at how I might implement this behavior? I was hoping to implement this by updating the constraints when scrolling up and scrolling down happens. How can I implement this?
UICollectionView subclasses UIScrollView, so you can implement a UIScrollViewDelegate to get the scroll events.
Here is some code I use in one of my apps
The bottom of the text field is constrained to the top of the collection view
The top of the text field is constrained to the view's top layout guide - This is the constraint that is modified by the scroll view delegate code
As the collection view scrolls up the text field will move up and fade out
extension ViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let textHeight = self.textField.frame.size.height + 2
let offset = min(scrollView.contentOffset.y, textHeight)
if offset >= 0 || self.textFieldConstraint.constant != 0 {
self.textFieldConstraint.constant = -offset
let percent = max(1 - offset/textHeight,0)
self.textField.alpha = percent
}
}
}
Essentially you "push" the text field off the top of the screen, no more than its height+2
I think this solution can work full for you try this, just add a UIScrollView view as parent and put your text field and collection view in scroll view, as set the constraint as you set.
having issues with UIScrollView. I have a setup like so:
I have a scrollview pinned in the first image, trailing, leading, top and bottom constraints. In the second image I have place a UIView of the same dimensions inside the scroll view (I plan to add content to this). This is pinned to the scroll view and also centred horizontally and vertically. It seems no touches are registered at all when I try to scroll now. I have set a large content size:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
print("The scroll views height is \(scrollView.frame.size.height)")
scrollView.contentSize = CGSize(width: scrollView.frame.size.width, height: 1000)
print("The scroll view content height is: \(scrollView.contentSize.height)")
}
I have also enabled user interaction everywhere I can. I am using the delegate method scrollViewDidScroll(scrollView: UIScrollView) to check if touches are being registered and they aren't at all. What am I doing wrong here?
Hope this will help you,Many facing this problem i hope my solution will provide relief to Devs.
your view Hierarchy should be like this :-
View (main view of my UIViewController) – with
-ContainerScrl
--Scroll View (UIScrollView)
---ViewInsideScrl
----Content1
----Content2 (etc)
As, the ScrollView only Scroll When its Content Size will be greater then the frame of Scrollview.
Now Comes the imp. part the constraint Should be like:-
Give constraint to the ContainerScrl and then Scroll View Should be pinned from all the direction to the ContainerScrl and ViewInsideScrl should be pinned to Scroll View. Now it will be giving warning like scrollable content size Ambiguity.
Look, if u give constraint like width and height of ViewInsideScrl should be equal to ContainerScrl, all the constraint error msg will be vanished but it will not scroll as frame getting equal to content size,, let suppose u want it to scroll in horizontal dirction then just give equal height constraint to both the view and give proportional constraint to ViewInsideScrl width constraint w.r.t ContainerScrl like
ViewInsideScrl = 2* ContainerScrl ;
it will make the content of scroll bigger then the frame.
Lets try this, if problem not get solved we will look further to it.
I'm having trouble getting this working, best explained what i am trying to do with this image of my storyboard.
The main goal is to make that container scrollable, with its size dependent on the view controller that gets added into it via code.
Here is how i have laid out my views.
Obviously my constraints are what is messing me up, but not exactly sure how to get them to behave as expected, I've tried pinning the container to the scroll view and then the scroll view to the main view. The screen appears how i want but it just doesn't scroll.
Any help is much appreciated!
It's impossible from your question & screenshots to determine what constraints you actually do have set up. And your question doesn't even particularly make it clear whether your constraints are behaving properly (aside from the scroll view not scrolling, which may or may not be related to constraints).
First, we need to make sure our constraints are hooked up correctly. The container view for which you're going to put a scroll view into should have four constraints. One for each side, left & right, pinning it to those edges. One for the bottom to pin it to the toolbar, and one for the top to pin it to your top views. It doesn't need any more constraints. Now, the toolbar at the bottom and your views at the top need to have their constraints set up so that they have a constant height per device/orientation, and does not care about the size of the container view. If your constraints are set up correctly, then resizing this view in your interface builder file should be changing the size of the container view. If this isn't happening, head back to the drawing board because something isn't quite right.
If you are confident your constraints are set up correctly, there are a few other things about the scrollview itself that could prevent scrolling.
First, check the most obvious. Is the scrolling enabled property set to true on your scroll view? If not... of course it's not going to scroll.
The other thing that could be happening is that your content view is not larger than the scroll view. Make sure that you've properly set up autolayout for the contents of the scroll view, otherwise the content view will not be larger than the scroll view, and no scrolling will be happening.
So for anyone wondering how to do this, i finally figured out how to do it after a lot of trial and error.
Basically what I did was remove the container view from the storyboard, set my constraints on the scroll view as i normally would:
and then added a container view as a subview of the scroll view in code:
var containerView : UIView!
var currentViewController: UIViewController
override func viewDidLoad() {
super.viewDidLoad()
self.containerView = UIView()
self.scrollView.addSubview(self.containerView)
}
To swap between different view controllers using my UISegmentControl and have the scroll view scroll the content no matter how large the new view controller is i added the following code in my UISegmentControl functions
#IBAction func segControlValueChanged(sender: AnyObject) {
if let vc = viewControllerForSelectedSegmentIndex(sender.selectedSegmentIndex) {
self.currentViewController!.view.removeFromSuperview()
self.currentViewController!.removeFromParentViewController()
displayCurrentTab(sender.selectedSegmentIndex)
}
}
func displayCurrentTab(tabIndex: Int){
if let vc = viewControllerForSelectedSegmentIndex(tabIndex) {
self.addChildViewController(vc)
self.containerView.addSubview(vc.view)
vc.didMoveToParentViewController(self)
vc.view.frame.size = CGSize(width: self.view.frame.width, height: vc.view.frame.height) //had to add this because otherwise for some reason my new vc width would not increase or decrease to its parent view
self.currentViewController = vc
self.scrollView.contentSize = vc.view.frame.size
}
}
func viewControllerForSelectedSegmentIndex(index: Int) -> UIViewController {
//instantiate and return your view controller here
}
Some other notes:
I disabled "resize view from NIB" in storyboard for all my view controllers i plan to add to the container
I have a button in a view which is in the footer of a tableview (UITableViewController). Why is the button stretching when I try to apply the following code to it?
And I apply the code:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
// Make footerview so it fill up size of the screen
// The button is aligned to bottom of the footerview
// using autolayout constraints
self.tableView.tableFooterView = nil
self.footerView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.tableView.frame.size.height - self.tableView.contentSize.height - self.footerView.frame.size.height)
self.tableView.tableFooterView = self.footerView
}
I was following the answer on this question:
Add button on top of UITableViewController (Swift)
Thanks!
If you read the question (whose link you posted) carefully, you can see that he resizes the view to take up the rest of the screen that is remaining after your table. Your button is the same size as your view, that is why it is stretching up. You need to add constraints which bind your button to the bottom of the view but not the top. Because if you bind the top and the bottom both to the view it will stretch.
Here is the example.
1. When you do not bind the button to the top. Notice that in the constraints, I do not have any constraint that specify the top of the button.
When you bind your button to the the top of the view. In this, I set a constraint which bind the button top to the view top. It stretches my button to take up the whole space as the view(which is similar to your case)
Hope this helps. :)