NS_OPTIONS 主要用于多选选项,表示一个选项集合
我们看下 UIView的声明
typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
UIViewAutoresizingNone =0,
UIViewAutoresizingFlexibleLeftMargin =1<<0,
UIViewAutoresizingFlexibleWidth =1<<1,
UIViewAutoresizingFlexibleRightMargin =1<<2,
UIViewAutoresizingFlexibleTopMargin =1<<3,
UIViewAutoresizingFlexibleHeight =1<<4,
UIViewAutoresizingFlexibleBottomMargin =1<<5
};
使用的时候,可以联合使用,多选的
// 以Iphone4(320, 480)为基础,设置各控件的位置// 注意:必须所有控件都按照Iphone4(320, 480)为基础初始化一次,不然按比例缩放时会发生错误!UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(kMargin, kTopSpace, kTopViewWidth, kTopViewHeight)];
CGFloat textLabelTop = (topView.frame.size.width - kTextLabelWidth) /2;
CGFloat textLabelWidth = (topView.frame.size.height - kTextLabelHeight) /2;
UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(textLabelTop, textLabelWidth, kTextLabelWidth, kTextLabelHeight)];// 设置文字及居中[textLabel setText:@"Garvey"];
[textLabel setTextAlignment:NSTextAlignmentCenter];// 分别设置样式[topView setBackgroundColor:[UIColor redColor]];
[textLabel setTextColor:[UIColor whiteColor]];// 设置文字控件的宽度按照上一级视图(topView)的比例进行缩放[textLabel setAutoresizingMask:UIViewAutoresizingFlexibleWidth];// 设置View控件的宽度按照父视图的比例进行缩放,距离父视图顶部、左边距和右边距的距离不变
NSUInteger autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin;
[topView setAutoresizingMask:autoresizingMask];
//通过 & 来判断是否包含:
if(autoresizingMask & :UIViewAutoresizingFlexibleWidth) {
包含 UIViewAutoresizingFlexibleWidth
}else{ //不包含UIViewAutoresizingFlexibleWidth }
//通过| 增加选项
autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
//减少选项
autoresizingMask = autoresizingMask & (~UIViewAutoresizingFlexibleWidth);
扩展知识点:关于枚举的小知识
//typedef NS_OPTIONS(NSInteger, myType) {
// type1 = 1 << 0,
// type2 = 1 << 1,
// type3 = 1 << 2,
// type4 = 1 << 3,
//};
/**
type1 = 1 << 0 :值为1(2的0次方)
type2 = 1 << 1 :值为2(2的1次方)
type3 = 1 << 2 :值为4(2的2次方)
type4 = 1 << 3 :值为8(2的3次方)
*/
网友评论