Skip to content
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
5 changes: 5 additions & 0 deletions UIImageViewAligned/UIImageViewAligned.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ typedef UIImageViewAlignmentMask UIImageViewAignmentMask __attribute__((deprecat
@property (nonatomic) BOOL alignTop;
@property (nonatomic) BOOL alignBottom;

@property (nonatomic) CGFloat insetLeftPercentage;
@property (nonatomic) CGFloat insetRightPercentage;
@property (nonatomic) CGFloat insetTopPercentage;
@property (nonatomic) CGFloat insetBottomPercentage;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are called inset but they seem to behave like outset? A positive value will push the image outwards.

Also, I'd prefer UIEdgeInsets as that's a standardized way to refer to paddings.
The way I would see this is the UIEdgeInsets would restrict the boundaries in which the alignment will happen.


// Make the UIImageView scale only up or down
// This are used only if the content mode is Scaled
@property (nonatomic) BOOL enableScaleUp;
Expand Down
10 changes: 6 additions & 4 deletions UIImageViewAligned/UIImageViewAligned.m
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,22 @@ - (void)setAlignment:(UIImageViewAlignmentMask)alignment

- (void)layoutSubviews
{
[super layoutSubviews];

CGSize realsize = [self realContentSize];

// Start centered
CGRect realframe = CGRectMake((self.bounds.size.width - realsize.width)/2, (self.bounds.size.height - realsize.height) / 2, realsize.width, realsize.height);

if ((_alignment & UIImageViewAlignmentMaskLeft) != 0)
realframe.origin.x = 0;
realframe.origin.x = 0 - (self.bounds.size.height * self.insetLeftPercentage);
else if ((_alignment & UIImageViewAlignmentMaskRight) != 0)
realframe.origin.x = CGRectGetMaxX(self.bounds) - realframe.size.width;
realframe.origin.x = CGRectGetMaxX(self.bounds) - realframe.size.width + (self.bounds.size.height * self.insetRightPercentage);

if ((_alignment & UIImageViewAlignmentMaskTop) != 0)
realframe.origin.y = 0;
realframe.origin.y = 0 - (self.bounds.size.height * self.insetTopPercentage);
else if ((_alignment & UIImageViewAlignmentMaskBottom) != 0)
realframe.origin.y = CGRectGetMaxY(self.bounds) - realframe.size.height;
realframe.origin.y = CGRectGetMaxY(self.bounds) - realframe.size.height + (self.bounds.size.height * self.insetBottomPercentage);

_realImageView.frame = realframe;

Expand Down