【问题标题】:iOS: AutoresizingMask using an array?iOS:使用数组的 AutoresizingMask?
【发布时间】:2011-08-19 01:23:26
【问题描述】:

是否可以为 UIView 的 autoresizingMask 属性提供一个数组?我想这样做的原因是我有一些条件可以确定我想将哪些 autoresizingMask 属性添加到我的视图中。

所以,不要仅仅使用:

self.view.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;

我想做这样的事情:

if (addMargin) {
   [items addObject:UIViewAutoresizingFlexibleRightMargin];
}
if (addWidth) {
   [items addObject:UIViewAutoresizingFlexibleWidth];
}

// Add to property
self.view.autoresizingMask = items;

所以我基本上是想有条件地设置这个属性的项。

【问题讨论】:

    标签: objective-c ios uiview autoresizingmask


    【解决方案1】:

    这是一个位掩码。只需按位或与您想要的那个。

    if(addMargin)
        self.view.autoresizingMask |= UIViewAutoresizingFlexibleRightMargin;
    if(addWidth)
        self.view.autoresizingMask |= UIViewAutoresizingFlexibleWidth;
    

    要重置掩码,可以将其设置为 0,或者如果要删除特定属性,可以将其取反并按位与掩码一起使用:

    if(removeMargin)
        self.view.autoresizingMask &= ~UIViewAutoresizingFlexibleRightMargin;
    

    【讨论】:

      【解决方案2】:

      自动调整大小只是一个位掩码。

      UIViewAutoresizing resize = 0;
      if (addMargin) {
          resize = resize | UIViewAutoresizingFlexibleRightMargin;
      }
      if (addWidth) {
          resize = resize | UIViewAutoresizingFlexibleWidth;
      }
      
      self.view.autoresizingMask = resize
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-12-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-02
        相关资源
        最近更新 更多