UICollectionView header width - ios

In my UICollectionViewFlowLayout subclass I have this:
self.headerReferenceSize = CGSizeMake(280, 44);
However the header is displayed with a width of 320, which is the width of the collection view. This is correct according to the docs:
During layout, only the size that corresponds to the appropriate
scrolling direction is used. For example, for the vertical scrolling
direction, the layout object uses the height value returned by your
method. (In that instance, the width of the header would be set to the
width of the collection view.)
However, I need the width to be 280, and not stretch to the entire width of the collection view. How can I override this?

You can add another custom view over header and resize it as you wish. Ofcourse all you're components that were on header will go over custom view you created...

You don't need to add a subview to the header.
You want a header width of 280 in a Collection View of 320, so apply a Content Inset of 20 on the left and right, like so:
Swift
YourCollectionView.ContentInset = UIEdgeInsets(top: 0, left: 20, bottom: 20, right: 0)
Xamarin
YourCollectionView.ContentInset = new UIEdgeInsets(0, 20, 0, 20);

Related

UITableView Cell should not scroll horizontally swift iOS

I have a table view which has card kind of cells as shown in picture.
I have set the content Inset so that I can get the cells with 20px spacing in left,right and top.
tableVw.contentInset = UIEdgeInsets(top: 20, left: 20, bottom: 0, right: 20)
Its displaying as I expected but the problem is, cells can be moved in all direction. But when I move it to right/top, it automatically comes back to original position. But when I move to left it just goes inside and don't scroll back to original position as shown in picture.
I don't want the cells to move at all or I want the cells to come back to centre if its dragged anywhere also. Please help!
Dont provide uiedgeinsets to tableview instead add a view in uitableview cell that cover up the whole cell and add another uiview inside that view and give constraint from top bottom leading trailing equals to 8 or whatever you want then the cell wont move anyways and u tableview cells will look like it has edgeinsets.
you need to set the clipsToBounds property true
tableview.clipsToBounds = true
If you're using AutoLayout, by setting this only should work for you:
In code:
tableView.alwaysBounceVertical = false
or In Interface Builder:
Just find this option and untick "Bounce Vertically" option.
Here's the reference:
If you're not using AutoLayout:
override func viewDidLayoutSubviews() {
// Enable scrolling based on content height
tableView.isScrollEnabled = tableView.contentSize.height > tableView.frame.size.height
}
and also try clipToBounds
tableview.clipsToBounds = true
I achieved what I wanted by below code.
postsTable.contentInset = UIEdgeInsets(top: 20, left: 20, bottom: 0, right: -20)
And in UITableViewCell class :
override var frame: CGRect{
get {
return super.frame
}
set(newFrame){
var frame = newFrame
frame.size.width = kScreenWidth - 40
super.frame = frame
}
}
Now if I drag the cell left or right also its coming back to original position.

swift: how to adjust table view's height to fit super view's height

I have a viewController into which I dragged a tableView, but when I load data in the tableView I get a scroll, what I want is to extend this tableView to its height to fill its superView. I tried to resize the tableView by setting tableView.frame.height = tableView.contentSize.height in viewDidAppear and it worked but I don't get a scroll in the superView, the tableView just got expanded and the content is down the view but I cant scroll, I tried to put this tableView inside a scrollView but still the same thing, what can I do?
yourTableview.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
Basically what you did was adjust the height of the tableView to be is the height if its content rather than the height of the screen (the content height is larger than the screen)
The table should carry the height of the screen (or any height you want) and if your content is bigger than the frame of your tableView it will automatically have the scrolling enabled.
Hope this helps!
If you want your scrollview to be scrollable, you need to indicate its contentSize by dynamically calculating the height of its nested components:
scrollView.isScrollEnabled = true
scrollView.contentSize = imageView.frame.height + tableview1.frame.height + tableview2.frame.height + netLabel.frame.height
Notice that you may also have to include your margins' height
I just needed to add the constraint of height of my table as an Outlet like this #IBOutlet weak var myConstraint: NSLayoutConstraint!, and the do myConstraint.constant = myTable.contentSize.height in viewDidAppear

TableView ContentInset does not shrink cell width / Add horizontal padding to TableView contents

I'd like to have a UITableView which is full screen. But the content of the UITableView should have a padding on the left and right.
So I tried to set ContentInset. But now the cells are as wide as the UITableView and the UITableView scrolls horizontally.
Is there a way to say that the UITableView content's width should become narrowed by the horizontal content insets? Or do I have to add the padding to all cells and header/footer views?
I don't want to narrow the table view itself, because the scroll indicator should stay at the right side of the screen and not in the middle.
The here (How to set the width of a cell in a UITableView in grouped style) suggested solution seems to be not as generic as i'd love to, beacuse the cells and header and footer views have to know about the padding (at least 3 places to maintain instead of one)
I don't want to narrow the table view itself, because the scroll
indicator should stay at the right side of the screen and not in the
middle.
This makes you happy?
_tableView.clipsToBounds = NO;
_tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 0, -30.f);
If you don't like clipsToBounds = NO effects, you can embed the tableView in container view which is clipsToBounds = YES.
Set the layout margins of the table view. For this to work make sure your constraints in the cells are set relative to the superview margin.
tableView.layoutMargins = UIEdgeInsets(top: 0, left: 40, bottom: 0, right: 40)

UITextView content inset

I have encountered something a bit strange with contentInsets
I have a UITextView in my storyboard with a contentInset of 50 left, as I'm trying to add some padding to my uitextview
However, a scrollbar appears on the bottom of the uitextview, as shown below in this test:
I was under the impression that contentInset squashes the uitextview without causing this horizontal scroll bar, so how can I remove the need for the horizontal scrollbar and make everything--the inset AND all the text in the uitextview--visible without the need for this scrollbar.
N.B: I'm not asking about preventing the scrolling horizontally or not displaying the scrollbar(thus cutting of the text)
Thanks a lot!
For atomk(UITextView is called ss)
NSLog(#"Content Size Before %f",self.ss.contentSize.width); Logs: 280
CGSize size=self.ss.contentSize; size.width=size.width-50;
[self.ss setContentSize:size];
NSLog(#"Content Size After %f",self.ss.contentSize.width); Logs: 230
There is no visible difference between the view with the code added than before it was added, so something's going wrong!
(Thanks)
In iOS 7 UITextView is based on TextKit and has a new property textContainerInset. It behaves just as you would expect:
UITextView *textView = ...;
// Left inset of 50 points
textView.textContainerInset = UIEdgeInsetsMake(0.0, 50.0, 0.0, 0.0);
Swift 4.2
textView.textContainerInset = UIEdgeInsets(top: 0, left: 50, bottom: 0, right: 0)
UPDATE: This solution is out of date as of iOS 7.
See this answer below. In iOS 7.0, the textContainerInset property on UITextView was introduced.
Objective-C:
textView.textContainerInset = UIEdgeInsetsMake(0, 50, 0, 0);
Swift:
textView.textContainerInset = UIEdgeInsets(top: 0, left: 50, bottom: 0, right: 0)
Or as Zeev Vax suggested, in Swift 5.0:
textView.textContainerInset.left = 50
Pre-iOS 7 solution:
I was under the impression that contentInset squashes the uitextview without causing this horizontal scroll bar...
I'm afraid this is not how contentInset works with a UITextView. See Apple's documentation for contentInset where it states:
The distance that the content view is inset from the enclosing scroll view... Use this property to add to the scrolling area around the content.
The contentInset is added around the content.
You can change the contentSize in viewDidLayoutSubviews using the code you have included above:
- (void)viewDidLayoutSubviews
{
self.textView.contentInset = UIEdgeInsetsMake(0, 50, 0, 0);
NSLog(#"Content Size Before %f",self.textView.contentSize.width); //Logs: 280
CGSize size=self.textView.contentSize;
size.width=size.width-50;
[self.textView setContentSize:size];
NSLog(#"Content Size After %f",self.textView.contentSize.width); //Logs: 230
}
However, this causes the text to be cut off on the right side:
The best way I have been able to achieve the appearance of horizontal padding in a UITextView is to position it inside a container UIView. In your case, simply create a UIView the same size as your current text view, and add a text view that is 50px narrower inside the container view.
This workaround can cause problems if you have a background for your text view, but from your screenshot above it doesn't look like that's an issue for you.
UITextView (frame in red) inside UIView container:
If your UITextView does have a background, see:
How to set UITextView's content inset like Notes App
Stuff you learn from reverse-engineering Notes.app (see "iPadding" section)

Overriding Default Table Width

Is there a way to manually set a UITableView's width?
I would like to set the table as a subview which takes up half of the width of the screen and the full height.
Sure you can set the table frame to whatever you want.
self.tableView.frame = CGRectMake(80, 0, 160, screenHeight);

Resources