what is the similar of linear layout in ios
Asked Answered
O

8

10

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.

Ontogeny answered 13/5, 2013 at 12:40 Comment(0)
J
11

iOS9 introduced something similar to LinearLayout: UiStackView

See this video from WWDC 2015: https://developer.apple.com/videos/wwdc/2015/?id=218

Jonajonah answered 12/6, 2015 at 16:49 Comment(2)
Finally... :) But, unlike on Android, Apple's new UI elements are NOT backwards compatible. Which means, UIStackView will be available only for iOS 9+...Octillion
UiStackView is poor man's LinearLayout to be honest, can't set per-tem weights and margins, etc.Request
N
1

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];
Neap answered 1/10, 2013 at 2:0 Comment(0)
C
1

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.

Cece answered 5/6, 2014 at 19:29 Comment(0)
S
1

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.

Sweltering answered 27/4, 2016 at 15:16 Comment(0)
E
1

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
Evvy answered 2/12, 2019 at 7:36 Comment(0)
C
0

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

Cher answered 13/5, 2013 at 12:46 Comment(0)
W
0

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.

Waterloo answered 13/5, 2013 at 15:1 Comment(0)
I
-2

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

Intercessory answered 13/5, 2013 at 12:45 Comment(3)
even thought, one of this component is a textview which its height is dynamic and bigger than the UIScrollView, in this case what shall I do?Ontogeny
UIScrollView's content size you can extend dynamically both horizontal and vertical directions. The textView is also a subclass of scrollview. Look into some scrollview tutsMorganstein
have you a link for some tutorials ? yes what i need is to extend dynamically the content size.Ontogeny

© 2022 - 2024 — McMap. All rights reserved.