How to make horizontal UICollectionView with Swift - swift

I want to display a list of product descriptions inside collection view cells and I'm using dequee for that.
I want to make sure that it only scrolls horizontally and not vertically at all.
The cell's width is 180px and the total number of my items is different every time.
I have tried a lot but the scrollView doesn't extend it's width at all.

You should specify the layoutDirection property of your collection in viewDidLoad:
if let flow = collectionView?.collectionViewLayout as? UICollectionViewFlowLayout{
flow.scrollDirection = .Horizontal
}
or you can do this in Storyboard.

For swift 5 and easy way to do it. Use auto layout

Related

Trying to add two images in a UI image

Im new to xcode and was trying to add different images in a table row in a storyboard layout.
let image:UIImage = UIImage(named:
”ster.jpg")!
when i use this code, it makes every table the same image
let image:UIImage = UIImage(named:
”ster.jpg", "blue.jpg")!
My goal is for the 2nd row of the table to have a different image, so when using this code it just creates an error saying " Extra argument in call"
I would start by saying that the way you initialize the UIImage object is wrong. It supports only one image at a time. Thus, you get the error of an extra argument in the call.
To add two images in a row, you better use a horizontal stack view and modify some of its properties (fill equally, spacing, etc).
The way I would do the thing: Drag and drop from library 2 UIImage objects. Embed them in a horizontal stack view. In property inspector set the fill property to equal. Make sure to add constraints.
In case you are using UITableView and want to modify the cell, you will have to create a subclass of UITableViewCell and give it an identifier to use when the TableViewDataSourceDelegate object is fetching the cell data based on the identifier.

How do I fit a paragraph in a scroll view controller programmatically?

I'm building an app and it has a description of everyone on the roster section. The text seems to overrun the page and I do not know the code or way to make it fill programmatically.
I have tried changing the values on the line of code the UITextField is in.
let descriptionM = UITextField(frame: CGRect(x: xPosition+15, y:
500, width: 300, height: 50))
descriptionM.text = desc[i]
descriptionM.textColor = UIColor.black
descriptionM.backgroundColor = UIColor.clear
descriptionM.contentMode = .scaleAspectFill
I wanted the text which is an array above, to fill the certain page on the scroll view instead it runs over the right side of the view with only the first line readable.
I have tried different values but non have worked.
I have tried changing the values on the line of code the UITextField is in.
You're using the wrong tool for the job. Text fields only ever handle one line, which is why there are no properties that let you set the number of lines.
I wanted the text which is an array above, to fill the certain page on the scroll view instead it runs over the right side of the view with only the first line readable.
Views that display text on multiple lines include UILabel and UITextView. UILabel is really meant for just a few lines or less, and it sounds like you may need many lines, so you should switch to UITextView. Or, if you're displaying a number of distinct items rather than paragraphs of text, you could use a UITableView where each cell displays a single item using a UILabel, UITextField, or UITextView. Table views are great for presenting arrays of data.

How to set margin for cell in tableView?

I tried to set a margin for a certain cell in my TableView by using code as below:
cell.layoutMargin.left = 20
However, when I launched the application, it changes nothing on the appearance. Is there any way I could achieve this?
For margins to take effect in any UIView, any constraints must have the "Constrain to margins" checked. Otherwise, the margins will not change any subview's constraints on the superview.
If you are adding constraints with Swift, here is an example of adding constraints relative to margins from Apple. The key part is:
// Get the superview's layout
let margins = view.layoutMarginsGuide
// Pin the leading edge of myView to the margin's leading edge
myView.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
Ensure that your view is being updated and that any changes you are making to UIView is from the main thread.
To give a more thorough answer, more data is needed on your current auto layout setup.
I found a solution or alternative for this problem after getting help from my colleague, what we did was creating an IBOutlet for the constraint (which I just discover). So in UITableviewCell class, i add this line of code :
#IBOutlet var boxLeadingContraint: NSLayoutConstraint!
And in UITableView I set the margin for the specified cell like this:
cell.boxLeadingContraint.constant = 10
Note: I did this on a UIImageView inside the cell not on the cell itself. Any feedback or advice will be appreciated.

how to remove a view from stackview and distribute the other fillequally

i have a stackview desing in storyboard with 5 view inside composed of 1 buttom, a small view with a label and a button. like this.
what i want is to remove one of the the view let say the one with orange background.
i tried this on viewdidload
stackview.view2.isHiden = true
stackview.view2.removeFromSuperview()
this remove the view and all elements but the remaing does not distribute as expected any idea how to achive this
// Appears to remove the first arranged view from the stack.
// The view is still inside the stack, it's just no longer visible, and no longer contributes to the layout.
let firstView = stackView.arrangedSubviews[0]
firstView.isHidden = true

Scrolling two UITableViews together

I have a weird design case in which to implement I need to place two UITableViews on the same view. I would just use sections, but I need an index on the second table.
I'd like them to both scroll together like they were actually two sections of the same table view. Is this possible?
Here's a basic mockup illustrating why:
As far as I understand it, you cannot move the index. Also, when you add an index to a table, it appears over the entire tableview, not just one section.
If I have to implement this as two table views, they both need to scroll as if they were one. If it's possible to do this using sections, that's even better.
Update
After seeing your update my old answer is not very good but I'll leave my previous answer for anyone else who wants it.
New answer - It depends how you want the tables sync'd.
If you know
Which cells are in the top 5
How tall each cell is
The offset the table view
Then you can calculate which cell is visible at the top of the top 5 table. You can use this information to scroll the bottom table to the correct index.
Old answer
I'm with the other guys not sure why you would want to or if I am misinterpreting but it's pretty simple.
UITableView is a subclass of UIScrollView so you can set yourself as the UITableViewDelegate and override
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
{
UITableView *slaveTable = nil;
if (self.table1 == scrollView) {
slaveTable = self.table2;
} else if (self.table2 == scrollView) {
slaveTable = self.table1;
}
[slaveTable setContentOffset:scrollView.contentOffset];
}
This gives me something that looks like this:
The top UITableView is self.table1 and the bottom is self.table2 if either table is scrolled the scrolling is mirrored in the other table.
in swift this code:
func scrollViewDidScroll(scrollView: UIScrollView) {
if tb_time1 == scrollView {
tb_time2.contentOffset = tb_time1.contentOffset
}else if tb_time2 == scrollView {
tb_time1.contentOffset = tb_time2.contentOffset
}
}
From the mock-up it seems like this can be accomplished with a single UITableView with two sections.
Instead of maintaining two tableViews you could maintain two collections (one for each section). Then in cellForRowAtIndexPath choose the collection to use based on the section specified in the indexPath.