簡體   English   中英

設置UIButton的大小與內容無關

[英]Setting Size of UIButton Independent of Content

我正在創建帶有邊框的UIButtons行,其標題將設置為不同的數字。 確實如此,所有按鈕均根據內部文本的長度自行調整大小,但我希望它們的大小均相同,比容納文本所需的大小大一點,因為這樣看起來會更好。

我使用子類在按鈕中添加了邊框,在其中指定了以下內容:

self.layer.cornerRadius = 5.0;
self.layer.borderColor = UIColor.blackColor().CGColor
self.layer.borderWidth = 1
self.backgroundColor = UIColor.whiteColor()
self.tintColor = UIColor.blackColor()

有沒有一種方法可以絕對地指定定義按鈕周圍邊框的矩形的大小? 我嘗試將self.frame設置為CGRect,但這似乎根本不影響邊框。

您正在使用情節提要嗎?

如果是,請按住Ctrl並將您的按鈕拖動到另一個按鈕,使其寬度相等。

如果您不使用情節提要:

constraint = [NSLayoutConstraint
    constraintWithItem:myButton
        attribute:NSLayoutAttributeWidth
        relatedBy:NSLayoutRelationEqual
          toItem: otherButton
       attribute:NSLayoutAttributeWidth
      multiplier:1.0f
        constant:0];

讓我知道你是否有任何麻煩

嘗試uibutton的titleEdgeInsets屬性。 此處提供文檔: https : //developer.apple.com/library/ios/documentation/UIKit/Reference/UIButton_Class/#//apple_ref/occ/instp/UIButton/titleEdgeInsets

默認為零,這意味着它會適合

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM