测试测试 | ✓ |
Lang语言 | Obj-CObjective C |
许可证 | BSD 3.0 |
发布最新发布 | 2016 年 2 月 |
由 Mirego,Marc Lefrancois,Francois Lambert 维护。
MCUIViewLayout 是一个添加到 UIView
中的类别,以便更容易地布局视图。它提供了一些快捷方法来设置宽度、高度和位置,而无需总是依赖于使用 CGGeometry
函数。
目前所有方法都以 mc_
前缀命名。这是为了避免在 用 Objective-C 编程中所描述的冲突。
您已添加一个视图,需要将其放在所有其他视图之下或相反?无需与 superview
的 subviews
数组玩弄。
- (void)funWithZOrdering
{
UIView *view = [[UIView alloc] init];
[view mc_bringToFront];
[view mc_sendToBack];
}
只用 setFrame:
或 setBounds:
来更改大小是痛苦的,因为您需要保留定位信息。这里有一些方便的方法来完成此操作。
- (void)funWithSize
{
UIView *view = [[UIView alloc] init];
// set only width
view.mc_width = 42;
[view mc_setWidth:42];
// set only height
view.mc_height = 12;
[view mc_setHeight:12];
// Set both with and height
view.mc_size = CGSizeMake(42,12);
[view mc_setSize:CGSizeMake(42,12)];
}
只用 setFrame:
或 setBounds:
仅更改位置是怪异的,因为您需要保留大小。
- (void)funWithPositions
{
UIView *view = [[UIView alloc] init];
// X position
view.mc_xPosition = 44;
[view mc_positionAtX:44];
// Y position
view.mc_yPosition = 22;
[view mc_positionAtY:22];
// Set both x and y position
view.mc_origin = CGPointMake(44,22);
[view mc_setOrigin:CGSizeMake(44,22)];
// ... or use combo methods
[view mc_positionAtX:44 andY:22];
[view mc_positionAtX:44 andY:22 withWidth:42];
[view mc_positionAtX:44 andY:22 withHeight:12];
[view mc_positionAtX:44 withHeight:12];
}
- (void)funWithSuperview
{
UIView *view = [[UIView alloc] init];
// +---------------------+
// | *** |
// | *** |
// | |
// +---------------------+
[view mc_positionRightOfSuperViewWithSpacing:5];
// +---------------------+
// | *** |
// | *** |
// | |
// +---------------------+
[view mc_positionLeftOfSuperViewWithSpacing:5];
// +---------------------+
// | |
// | |
// |*** |
// |*** |
// | |
// | |
// | |
// | |
// +---------------------+
[view mc_positionTopOfSuperViewWithSpacing:2];
// +---------------------+
// | |
// | |
// | |
// | |
// |*** |
// |*** |
// | |
// | |
// +---------------------+
[view mc_positionBottomOfSuperViewWithSpacing:2];
// +---------------------+
// | |
// | |
// | *** |
// | *** |
// | |
// | |
// +---------------------+
[view mc_centerInSuperView];
// Centered minus 1/8th of the height of the superview
[view mc_centerInSuperView];
}
如果您更希望明确地指定位置,可以使用 MCViewPosition 和 UIEdgeInsets
,这得益于 UIEdgeInsetsMake(top,left,bottom,right)
和 UIEdgeInsetsZero
。
- (void)evenMoreFunWithPositioningAndSuperviews
{
UIView *view = [[UIView alloc] init];
// Just position
[view mc_setPosition:MCViewPositionCenter withMargins:UIEdgeInsetZero] // Same as mc_centerInSuperView
// Position and size in one.
[view mc_setPosition:MCViewPositionBottomRight withMargins:UIEdgeInsetMake(0,0,10,20) size:CGSizeMake(100,200)];
// Above is equivalent to:
// [view mc_setWidth:100];
// [view mc_setHeight:200];
// [view mc_positionRightOfSuperViewWithSpacing:20];
// [view mc_positionBottomOfSuperViewWithSpacing:10];
}
// MCViewPosition
typedef NS_OPTIONS(NSInteger, MCViewPosition) {
MCViewPositionHCenter = (0x1 << 0),
MCViewPositionVCenter = (0x1 << 1),
MCViewPositionTop = (0x1 << 2),
MCViewPositionBottom = (0x1 << 3),
MCViewPositionLeft = (0x1 << 4),
MCViewPositionRight = (0x1 << 5),
MCViewPositionTopLeft = MCViewPositionTop | MCViewPositionLeft,
MCViewPositionTopHCenter = MCViewPositionTop | MCViewPositionHCenter,
MCViewPositionTopRight = MCViewPositionTop | MCViewPositionRight,
MCViewPositionBottomLeft = MCViewPositionBottom | MCViewPositionLeft,
MCViewPositionBottomHCenter = MCViewPositionBottom | MCViewPositionHCenter,
MCViewPositionBottomRight = MCViewPositionBottom | MCViewPositionRight,
MCViewPositionVCenterLeft = MCViewPositionVCenter | MCViewPositionLeft,
MCViewPositionCenters = MCViewPositionVCenter | MCViewPositionHCenter,
MCViewPositionVCenterRight = MCViewPositionVCenter | MCViewPositionRight
};
- (void)funWithRelativePositioning
{
UIView *view = [[UIView alloc] init];
UIView *otherView = [[UIView alloc] init];
// centers "view" directly above "otherView" with no margin
[view mc_setPosition:MCViewRelativePositionAboveCentered relativeToView:otherView withMargins:UIEdgeInsetZero];
// position "view" left of "otherView" centered horizontally.
[view mc_setPosition:MCViewRelativePositionToTheLeftCentered relativeToView:otherView withMargins:UIEdgeInsetZero size:CGSizeMake(100,200)];
// center view inside otherview
[view mc_setPosition:MCViewPositionCenter inView:otherView withMargins:UIEdgeInsetZero]
// ... same with size
[view mc_setPosition:MCViewPositionCenter inView:otherView withMargins:UIEdgeInsetZero size:CGSizeMake(100,200)];
}
所有相对视图定位选项
typedef NS_ENUM(NSInteger, MCViewRelativePosition) {
MCViewRelativePositionAboveAlignedLeft,
MCViewRelativePositionAboveCentered,
MCViewRelativePositionAboveAlignedRight,
MCViewRelativePositionToTheRightAlignedTop,
MCViewRelativePositionToTheRightCentered,
MCViewRelativePositionToTheRightAlignedBottom,
MCViewRelativePositionToTheLeftAlignedTop,
MCViewRelativePositionToTheLeftCentered,
MCViewRelativePositionToTheLeftAlignedBottom,
MCViewRelativePositionUnderAlignedLeft,
MCViewRelativePositionUnderCentered,
MCViewRelativePositionUnderAlignedRight
};
这里并没有描述所有定位方法。这还是一个正在进行中的项目。您可以在 UIView+MCLayout.h
中找到所有定位方法。
如果您正在使用 CocoaPods
,这再简单不过了。将以下内容添加到您的 Podfile
中,并运行 pod install
pod 'MCUIViewLayout', :git => 'https://github.com/mirego/MCUIViewLayout.git'
别忘了在需要的地方包含 #import "UIView+MCLayout.h"
。
MCUIViewLayout © 2013-2015 Mirego,可自由分发并遵守 新BSD许可证。请查看 LICENSE.md
文件。
Mirego 是一支充满激情的人组成的团队,我们相信工作是一个可以创新和享受乐趣的地方。我们是一支 有才华的人 的团队,我们想象并构建美丽的Web和移动应用。我们聚集在一起分享想法并 改变世界。
我们也 热爱开源软件,并尽力为社区做出更多贡献。