I have many components in my view controller, and I want to combine them in a single object to scroll them together, what is the procedure to do it (like linearlayout in android) but I need it in IOS.
iOS9 introduced something similar to LinearLayout: UiStackView
See this video from WWDC 2015: https://developer.apple.com/videos/wwdc/2015/?id=218
Apple not provide linear container but you can use XHFLinearView at github
Usage Example:
XHFLinearView *linearView=[[XHFLinearView alloc]initWithFrame:self.view.bounds];
[self.view addSubview:linearView];
//init linear view content views
[linearView.itemSource addObject:XHFLinearViewUnitMake(someView, XHFMarginMake(0, 0, 0, 0))];
//force layout linear view
[linearView needLayout];
//insert a view with animation
[linearView insertItem:someView margin:XHFMarginMake(0, 0, 0, 0) atIndex:0 withAnimation:XHFLinearItemAnimationFade];
//replace a view with animation
[linearView replaceItem:someView withNewItem:newView withAnimation:XHFLinearItemAnimationFade];
//resize a view with animation
someView.frame=xxx;
[linearView needLayoutForItem:someView];
//remove a view with animation
[linearView removeItemByIndex:0 withAnimation:XHFLinearItemAnimationFade];
LayoutManager
First of all, what you're looking for is a declarative API for layouts. I guess a lot of people that comming from other languages/platforms miss that. iOS just has some other approaches like layout constraints or autoresizing. However this does not fit all the use cases.
Layout libraries
I would suggest to take a look into some libraries existing at CocoaPods. Its easy to install and kick off with it. I'd like to mention CSLinearLayoutView which is quite easy. Even I am maintainer of a smaller project MKLayoutLibrary which is available via CocoaPods as well. Thats a more compressive one which also provides a linear-, stack- and a simple flow-layout.
Clean solution
However, if you really want to go very deeply into the topic of layouts I recommend playing with UICollectionView and its layout customisation possibilities.
Consider that declarative solutions does not work for a huge amount of items or endless scrolling layouts. If you want to do so, take a further look into UICollectionView custom layouts.
I just wrote a widget called AutoLinearLayoutView which can be checked out from Github.
It's totally based on Auto-layout and supporting iOS 7+. According to your requirement, the example project perfectly demonstrates a dozen of widgets being wrapped by linear layout view, and the linear layout view being placed in UIScrollView.
Linear Layout in Android = Stack View in iOS
Android:
orientation: Horizontal, Vertical
iOS:
Horizontal Stack View, Vertical Stack View in iOS
More:
Recycler View in Android = Table View in iOS
There is no such layout available in iOS. Each component you add on the view will be displayed based on it's frame (not in sequential manner).
For doing this stuff you can use UIScrollView.
Check this tutorial How to use UIScrollView
You'll likely have to do something a bit more manual with iOS. You can add all your views to a UIScrollView
, however you'll have to set the contentSize
of the UIScrollView appropriately. Essentially, for N items, the scroll view width will be:
scrollView.contentSize = CGSizeMake(N * itemWidth + (N - 1) * itemPadding, itemHeight);
Then, you'll need to set the frame of each UIView
so that its x-coordinate is appropriate, and add it to the UIScrollView
:
for (int i = 0; i < views.count; i++) {
UIView *view = [views objectAtIndex:i];
view.frame = CGRectMake(i * itemWidth + i * itemPadding,
view.frame.origin.y,
view.frame.size.width,
view.frame.size.height);
[scrollView addSubview:view];
}
If you want something more complicated than a simple linear layout, have a look at UICollectionView
.
Every component as uiview in ios. so you can put every components into UIView like
[containerView addSubView:YourComponent1];
[containerView addSubView:YourComponent2];
[containerView addSubView:YourComponent3];
then you can add this into your UIScrollView
© 2022 - 2024 — McMap. All rights reserved.