What is the best / easiest way to create layout layouts in iOS

Q1. I have three controls: UILabel , UIButton and UILabel on the same line. I want to programmatically arrange them one by one in a line without any spaces (similar to the Java / Android "Flowlayout" layout), since the text length on each control will change as a result of user actions. What is the best / easiest way to program flowlayout programmatically?

Q2. In this regard, I want each control to automatically change when the text changes due to user actions, so the full text remains visible.

Thanks in advance.

// Edited 11/12/2011

Here's how I plan to achieve a horizontal "flow layout" of the controls that are contained in the Array view:

 -(void) doHorizontalFlowLayout:(NSArray *) viewArray { if(viewArray == nil || viewArray.count <=1 return; //get out of here, no need to continue UIView *v0= (UIView *) [viewArray objectAtIndex:0]; // first view CGRect frame0 = v0.frame; CGFloat sumWidth= 0; for(int i=1; i < viewArray.count; i++) { UIView *thisView= (UIView*) [viewArray objectAtIndex:i]; sumWidth = sumWidth+ v0.frame.size.width; CGRect nextFrame= CGRectMake(frame0.origin.x +sumWidth, thisView.frame.origin.y, thisView.frame.size.width, thisView.frame.size.height); thisView.frame= nextFrame; //the above works for 2 views only. For more than 2 views - reset v0 to point to the ith view v0 = (UIView*) [viewArray objectAtIndex:i]; } } 
+6
source share
3 answers

Just struggled with this - here is my solution:

IOS doesn't have the concept of a โ€œstream layout,โ€ so you need to calculate the width of each control manually and place them absolutely in the parent container. UILabels are slightly different from buttons, since the only way to determine the size of the content is by using the [NSString sizeWithFont:...] method. See here for an example.

In your case, you will need to listen when user interaction is completed depending on which item is changing, and use [UIView setNeedsDisplay] (http://developer.apple.com/library/ios/#documentation/UIKit/Reference/ UIView_Class / UIView / UIView.html # // apple_ref / occ / instm / UIView / setNeedsDisplay) to redraw the view and routines.

This is much more complicated than necessary, IMHO.

+5
source

Update for this answer: iOS6 introduced us to the Flow layout,

Creating dynamic and incredible interfaces is easy with the collection in iOS 6. Learn how to get started with collection lists and see how to easily organize data in grid layouts using the built-in UICollectionViewFlowLayout Class

Watch the WWdc 2012 video titled โ€œPresenting Collection Viewsโ€

+4
source

Try CocoaUI framework: https://github.com/ideawu/cocoaui - Create an iOS application interface with Objective-C + HTML + CSS and a web-style stream layout.

0
source

Source: https://habr.com/ru/post/899940/


All Articles