Skip to content

Two minor improvements - findByTag and hidden type support like android #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions CSLinearLayoutView/CSLinearLayoutView.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ typedef enum {

- (void)swapItem:(CSLinearLayoutItem *)firstItem withItem:(CSLinearLayoutItem *)secondItem;

- (CSLinearLayoutItem*)findItemByTag:(NSInteger)tag;
@end


Expand Down Expand Up @@ -85,14 +86,20 @@ typedef struct {
CGFloat right;
} CSLinearLayoutItemPadding;

NS_ENUM(NSInteger, CSLinearLayoutItemVisibility) {
CSLinearLayoutItemHidden, // Occupy the frame
CSLinearLayoutItemGone // Missing in the layout
};

@interface CSLinearLayoutItem : NSObject

@property (nonatomic, retain) UIView *view;
@property (nonatomic, strong) UIView *view;
@property (nonatomic, assign) CSLinearLayoutItemFillMode fillMode;
@property (nonatomic, assign) CSLinearLayoutItemHorizontalAlignment horizontalAlignment; // Use horizontalAlignment when the layout view is set to VERTICAL orientation
@property (nonatomic, assign) CSLinearLayoutItemVerticalAlignment verticalAlignment; // Use verticalAlignment when the layout view is set to HORIZONTAL orientation
@property (nonatomic, assign) enum CSLinearLayoutItemVisibility hiddenType; // Apply when the layout view is set YES hidden
@property (nonatomic, assign) CSLinearLayoutItemPadding padding;
@property (nonatomic, assign) NSDictionary *userInfo;
@property (nonatomic, weak) NSDictionary *userInfo;
@property (nonatomic, assign) NSInteger tag;

- (id)initWithView:(UIView *)aView;
Expand Down
38 changes: 23 additions & 15 deletions CSLinearLayoutView/CSLinearLayoutView.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

@interface CSLinearLayoutView()

@property (nonatomic, readwrite) NSMutableArray *items;

- (void)setup;
- (void)adjustFrameSize;
- (void)adjustContentSize;
Expand All @@ -22,7 +24,6 @@ @implementation CSLinearLayoutView
@synthesize orientation = _orientation;
@synthesize autoAdjustFrameSize = _autoAdjustFrameSize;
@synthesize autoAdjustContentSize = _autoAdjustContentSize;

#pragma mark - Factories

- (id)init {
Expand Down Expand Up @@ -59,10 +60,8 @@ - (void)setup {


#pragma mark - Lifecycle

- (void)dealloc {
[_items release], _items = nil;
[super dealloc];
self.items = nil;
}


Expand All @@ -75,6 +74,10 @@ - (void)layoutSubviews {

for (CSLinearLayoutItem *item in _items) {

if (item.view.hidden && item.hiddenType == CSLinearLayoutItemGone) {
continue;
}

CGFloat startPadding = 0.0;
CGFloat endPadding = 0.0;

Expand Down Expand Up @@ -166,6 +169,10 @@ - (CGFloat)layoutOffset {
CGFloat currentOffset = 0.0;

for (CSLinearLayoutItem *item in _items) {
if (item.view.hidden && item.hiddenType == CSLinearLayoutItemGone) {
continue;
}

if (_orientation == CSLinearLayoutViewOrientationHorizontal) {
currentOffset += item.padding.left + item.view.frame.size.width + item.padding.right;
} else {
Expand Down Expand Up @@ -210,12 +217,10 @@ - (void)removeItem:(CSLinearLayoutItem *)linearLayoutItem {
return;
}

[linearLayoutItem retain];

[_items removeObject:linearLayoutItem];
[linearLayoutItem.view removeFromSuperview];

[linearLayoutItem release];
}

- (void)removeAllItems {
Expand Down Expand Up @@ -264,12 +269,10 @@ - (void)moveItem:(CSLinearLayoutItem *)movingItem beforeItem:(CSLinearLayoutItem
return;
}

[movingItem retain];
[_items removeObject:movingItem];

NSUInteger existingItemIndex = [_items indexOfObject:existingItem];
[_items insertObject:movingItem atIndex:existingItemIndex];
[movingItem release];

[self setNeedsLayout];
}
Expand All @@ -279,7 +282,6 @@ - (void)moveItem:(CSLinearLayoutItem *)movingItem afterItem:(CSLinearLayoutItem
return;
}

[movingItem retain];
[_items removeObject:movingItem];

if (existingItem == [_items lastObject]) {
Expand All @@ -288,7 +290,6 @@ - (void)moveItem:(CSLinearLayoutItem *)movingItem afterItem:(CSLinearLayoutItem
NSUInteger existingItemIndex = [_items indexOfObject:existingItem];
[_items insertObject:movingItem atIndex:++existingItemIndex];
}
[movingItem release];

[self setNeedsLayout];
}
Expand All @@ -298,15 +299,13 @@ - (void)moveItem:(CSLinearLayoutItem *)movingItem toIndex:(NSUInteger)index {
return;
}

[movingItem retain];
[_items removeObject:movingItem];

if (index == ([_items count] - 1)) {
[_items addObject:movingItem];
} else {
[_items insertObject:movingItem atIndex:index];
}
[movingItem release];

[self setNeedsLayout];
}
Expand All @@ -323,6 +322,16 @@ - (void)swapItem:(CSLinearLayoutItem *)firstItem withItem:(CSLinearLayoutItem *)
[self setNeedsLayout];
}

- (CSLinearLayoutItem*)findItemByTag:(NSInteger)tag
{
for (CSLinearLayoutItem *item in _items) {
if (item.tag == tag) {
return item;
}
}
return nil;
}

@end

#pragma mark -
Expand Down Expand Up @@ -353,6 +362,7 @@ - (id)initWithView:(UIView *)aView {
self = [super init];
if (self) {
self.view = aView;
self.tag = aView.tag;
self.horizontalAlignment = CSLinearLayoutItemHorizontalAlignmentLeft;
self.verticalAlignment = CSLinearLayoutItemVerticalAlignmentTop;
self.fillMode = CSLinearLayoutItemFillModeNormal;
Expand All @@ -361,17 +371,15 @@ - (id)initWithView:(UIView *)aView {
}

+ (CSLinearLayoutItem *)layoutItemForView:(UIView *)aView {
CSLinearLayoutItem *item = [[[CSLinearLayoutItem alloc] initWithView:aView] autorelease];
CSLinearLayoutItem *item = [[CSLinearLayoutItem alloc] initWithView:aView];
return item;
}

#pragma mark - Memory Management

- (void)dealloc {
self.view = nil;
self.userInfo = nil;

[super dealloc];
}


Expand Down