• This repository has been archived on 07/Aug/2019
  • Stars
    star
    398
  • Rank 108,325 (Top 3 %)
  • Language
    Objective-C
  • License
    Other
  • Created over 9 years ago
  • Updated about 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple UIKit extension to wrap Flexbox layouts.

FlexboxKit

Checkout FlexboxLayout if you're interested in the Swift port

A simple UIKit extension to wrap the flexbox properties in regular UIView. This project is based on the robust Facebook's C implementation of Flexbox.

The goal is to have a small standalone UIKit library to layout elements. It doesn't rely on the DOM model at all.

![Gif](demo.gif)

#Usage

The easiest way to use the flexbox layout facilities is to instantiate a FLEXBOXContainerView, set its flexbox properties (as exposed in the UIView category UIVIew+FLEXBOX), add all the subviews you want to it and additionaly set their flex properties.

If you have subviews which themselves will have subviews that you wish to layout using the flexbox engine, you simply have to set the UIView category property flexContainer to YES, and so on. You can also have nested FLEXBOXContainerViews.

e.g. Given a view (in this case a UITableViewCell) with these subviews:

FLEXBOXContainerView *contentView;
UIView *left, *right;
UILabel *title, *caption;

...

[contentView addSubview:left];
[contentView addSubview:right];
[contentView addSubview:time];

[right addSubview:title];
[right addSubview:caption];

The following flexbox layout code

contentView.flexDirection = FLEXBOXFlexDirectionRow;

left.flexFixedSize = (CGSize){A_FIXED_SIZE, A_FIXED_SIZE};
left.flexMargin = (UIEdgeInsets){SOME_MARGIN, SOME_MARGIN, SOME_MARGIN, SOME_MARGIN};
left.flexAlignSelf = FLEXBOXAlignmentCenter;

rigth.flexContainer = YES;
right.flex = 1;
right.flexJustifyContent = FLEXBOXJustificationCenter;

time.flexMargin = (UIEdgeInsets){SOME_MARGIN, SOME_MARGIN, SOME_MARGIN, SOME_MARGIN};
time.flexPadding = (UIEdgeInsets){SOME_PADDING, SOME_PADDING, SOME_PADDING, SOME_PADDING};
time.flexAlignSelf = FLEXBOXAlignmentCenter;

Results in:

![Gif](cell-example.png)

##Advanced usage

You can use FlexboxKit without using FLEXBOXContainerView by simply having a -[UIView layoutSubviews] implementation that calls the -[UIView flexLayoutSubviews] method defined in the UIView category UIVIew+FLEXBOX.

e.g.

- (void)layoutSubviews
{
    [super layoutSubviews];
    [self flexLayoutSubviews];
}

If you wish to run the layout engine on a background thread you can do so by calling [node layoutConstrainedToMaximumWidth:self.bounds.size.width] in a background thread and then set the computed frames in the main thread.

e.g.

- (void)flexLayoutSubviewsInBackground
{
    __weak __typeof(self) weakSelf = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        
        __strong __typeof(self) strongSelf = weakSelf;

        //run the flexbox engine on a backgroun thread...
        strongSelf.flexNode.dimensions = strongSelf.bounds.size;
        [strongSelf.flexNode layoutConstrainedToMaximumWidth:strongSelf.bounds.size.width];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            //assign the computed frames on the main thread...
            for (NSUInteger i = 0; i < strongSelf.flexNode.childrenCountBlock(); i++) {
                
                UIView *subview = self.subviews[i];
                FLEXBOXNode *subnode = strongSelf.flexNode.childrenAtIndexBlock(i);
                subview.frame = CGRectIntegral(subnode.frame);
            }
            
            strongSelf.frame = (CGRect){strongSelf.flexNode.frame.origin, strongSelf.flexNode.frame.size};
        });
        
    });

}

Attribuitions

It uses Facebook's flexbox implementation and was inspired by Josh Abernathy's SwiftBox and Robert Bรถhnke's FLXView.

More Repositories

1

Render

UIKit a-lร  SwiftUI.framework [min deployment target iOS10]
Swift
2,157
star
2

ios-fontawesome

NSString+FontAwesome
Objective-C
1,751
star
3

ios-realtimeblur

Multipurpose real-time blur view for iOS6+
Objective-C
956
star
4

Store

Unidirectional, transactional, operation-based Store implementation.
Swift
502
star
5

Buffer

Swift ฮผ-framework for efficient array diffs and datasource adapters.
Swift
349
star
6

flexboxswfit_deprecated

(Deprecated) Port of Facebook's css-layout to Swift
Swift
307
star
7

DataStructures

A collection of Data Structures implemented in Swift.
Swift
48
star
8

Primer

Assign/Partial/ReadOnly/Proxy/Locks in Swift
Swift
32
star
9

CoreRender

Moved to https://github.com/alexdrone/Render
Objective-C++
26
star
10

S.swift

Get strong typed, autocompleted resources, color swatches and font styles in Swift projects from a simple YAML stylesheet.
Swift
25
star
11

YAS

Yet Another Stylesheet (YAML-based Stylesheet Engine)
Swift
23
star
12

Surface

A set of utilities and extensions to create depth in your views by using complex shadows.
Swift
18
star
13

PushID

Robust and thread-safe generator for uuids with guaranteed chronological ordering in Swift.
Swift
16
star
14

Emit

Event propagation and object observation library (Pre-Combine.framework).
Swift
10
star
15

ObjCTrait

Trait support for Objective-C
Objective-C
9
star
16

PatchComposer

A Quartz-composer like UI for patch editing in SwiftUI
Swift
8
star
17

SwiftUI_UIKit_Bridge

Share custom typography and color swatches between UIKit and SwiftUI.
Swift
6
star
18

Pbtxt

Schemaless protobuf text-format parser (with Codable support).
Swift
5
star
19

itu_pku_eventregexp

Java
5
star
20

GuruMeditation

Nostalgic error screen for iOS.
Swift
4
star
21

DesignSystemTemplate

A customizable design system in Swift.
Swift
4
star
22

Multipeer

Forked from: https://github.com/insidegui/MultipeerKit
Swift
3
star
23

ui_dsl_deprecated

(Deprecated) A lightweight native stylesheet engine for iOS
Objective-C
3
star
24

cxx_tutorials

A bunch of tutorials used to teach modern C++ and OpenGL to iOS devs
C++
3
star
25

Multi.kt

Kotlin Multiplatform Lib Template
Kotlin
3
star
26

swift1_rune_game

An iOS game inspired by 'Dots' based on the nordic runes
Objective-C
2
star
27

firebase_utils_deprecated

Firebase-backed store for Dispatch
Swift
2
star
28

trn_modelmapper

A ORM inspired by Ruby's DataMapper and ActiveRecord
Java
2
star
29

yaHN-android

Yet another HackerNews Android client
Java
2
star
30

FontCustom

fontcustom template for iOS
Objective-C
2
star
31

itu_algorithms

Some algorithms back from Uni time
Ruby
2
star
32

objc_latte_uikit_deprecated

(deprecated) An iOS markup language, framework and tool to help the design and the implementation of the presentation layer.
1
star