2011-08-18 73 views
2

是否可以向UIView的autoresizingMask屬性提供一個數組?我想這樣做的原因是我有一些條件決定了我想添加到我的視圖中的哪個autoresizingMask屬性。iOS:使用數組的AutoresizingMask?

因此,而不是隻是用:

self.view.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth; 

我想要做的事,如:

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

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

所以我基本上要有條件地設置該屬性的項目。

回答

6

這是一個有點掩碼。只需按位或 - 與你想要的那個。

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

要重置面具,你可以將其設置爲0,或者如果你想刪除某個特定的屬性,你可以否定它,按位與它的面膜:

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

它的工作完美!謝謝,這正是我想要的。 –

2

自動調整隻是一個位掩碼。

UIViewAutoresizing resize = 0; 
if (addMargin) { 
    resize = resize | UIViewAutoresizingFlexibleRightMargin; 
} 
if (addWidth) { 
    resize = resize | UIViewAutoresizingFlexibleWidth; 
} 

self.view.autoresizingMask = resize