2011-04-18 99 views
4

我創建了一個UIToolbar,只有一個UILabel放置在它上面。標籤內容是動態的,可以改變,我想將它集中在UIToolbar上。UIToolbar - 邊距?有誰知道UIToolbar的左右邊距?

有沒有人知道UIToolbar的左側和右側的確切邊距尺寸?

我創建一個[的UIBarButtonItem頁頭] initWithCustomView:myLabelContainer]我試圖以確保myLabelContainer佔據整個工具欄空白處,減去強制利潤率。

現在我已經猜測邊緣在每邊大約是12像素,所以UIView框架寬度爲296應該填充整個UIToolbar?

此信息可在任何地方使用嗎?

回答

9

我不認爲這個信息是可用的。 但它應該很容易得到一些測試。

如果你需要的只是整個工具欄上的UILabel,我建議只是將它添加到UIToolbar,這是一個UIView子類。你不需要你的案例中的所有UIBarButtonItem功能....只是我認爲的背景。

然後設置UILabel.frame(需要邊距)+ addSubview: + a UITextAlignmentCenter應該完成這項工作!另外,也許還與UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight一個autoresizingMask如果你有管理設備的方向......

編輯1

由於有關於提議有些疑惑,我做了一個簡單的測試:

UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 0, 300, 44)] autorelease]; 
label.text = @"Hello world"; 
label.textAlignment = UITextAlignmentCenter; 
label.textColor = [UIColor whiteColor]; 
label.shadowColor = [UIColor blackColor]; 
label.backgroundColor = RGBACOLOR(255, 0, 0, .3f); 
[self.toolbar addSubview:label]; 

下面是結果:

enter image description here

編輯2

現在我已經啓動了Xcode並編寫了一些代碼,這很容易確定您的主要問題。改變了一下以前的代碼:

UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease]; 
... same 
[self.toolbar setItems: 
[NSArray arrayWithObject: 
    [[[UIBarButtonItem alloc] initWithCustomView:label] autorelease]]]; 

帶來的是:

toolbar2

所以你猜到了,還有的12px的左邊界,但自定義視圖並不像被調整因此,在這種情況下,沒有權利保證金......除非您相應調整您的觀點。

編輯3

除非你需要的UILabel的背景下,這裏是我可能會做(這就是UIBarButtonSystemItemFlexibleSpace是畢竟...)

UILabel *label = [[[UILabel alloc] init] autorelease]; 
label.text = @"Hello world"; 
... same 
[label sizeToFit]; 
[self.toolbar setItems: 
[NSArray arrayWithObjects: 
    [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
               target:nil action:nil] autorelease], 
    [[[UIBarButtonItem alloc] initWithCustomView:label] autorelease], 
    [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
               target:nil action:nil] autorelease], 
    nil]]; 

其結果是:

enter image description here

EDIT 4

作爲一個方面沒有,在UIToolbar更加工後,12px是默認的空間加入之前一個按鈕。而我剛剛發現了一個有趣的,墊片正與負值!。 I.E.如果你想在兩個按鈕之間有一個2像素的空間,只需添加一個-10px UIBarButtonSystemItemFixedSpace ...方便!

+0

@ Vincent-G如果你的意思只是添加一個UILabel作爲子視圖,'[myToolbar addSubview:myUILabel]',那麼這是不正確的。請參閱:http://stackoverflow.com/questions/5126980/iphone-how-to-add-text-on-uitoolbar/5126997#5126997 – 2011-04-18 20:01:50

+0

該鏈接提到Interface Builder,我不使用,你試過添加標籤那樣? (啓動Xcode ...) – 2011-04-18 20:05:50

+0

添加了一個示例+截圖。這樣可行。 – 2011-04-18 20:20:19

0

似乎沒有任何地方存儲這個邊距,但是這裏有一個在Xamarin中計算的方法。首先一個按鈕添加到工具欄在你的ViewController:

UIButton button = new UIButton(); 
button.Tag = 999; 
ToolbarItems = new[] { FilterButton }; 

然後使用此代碼讀出(使用斷點)填充的價值爲任何特定的方位和設備。 (我已經包含了定位,因爲這會告訴你什麼是應用程序的方向目前是如果你是一個旋轉事件期間調用該代碼的變量。)

if (NavigationController != null) 
{ 
    UIButton firstButton = NavigationController.Toolbar.Subviews 
     .FirstOrDefault(x => x.GetType() == typeof(UIButton) && x.Tag == 999); 
    if (firstButton != null) 
    { 
     var orientation = UIApplication.SharedApplication.StatusBarOrientation; 
     var padding = firstButton.Frame.X; 
    } 
} 

爲了記錄在案,爲iOS9, iPhone上的值爲16,縱向爲20,iPad上則爲20。