簡體   English   中英

使用 NSLayoutConstraint 添加 UIButton

[英]Adding a UIButton using NSLayoutConstraint

我有下面的代碼。 但是我在視圖中沒有看到 UIButton。 幫助!

UIButton *addTag = [UIButton buttonWithType:UIButtonTypeRoundedRect];

[_overlayView addSubview: addTag];

[_overlayView setTranslatesAutoresizingMaskIntoConstraints:NO];


[_overlayView addConstraint:[NSLayoutConstraint constraintWithItem:_overlayView
                             attribute:NSLayoutAttributeCenterX
                             relatedBy:NSLayoutRelationEqual
                                toItem:addTag
                             attribute:NSLayoutAttributeCenterX
                            multiplier:1
                              constant:0]];

[_overlayView addConstraint: [NSLayoutConstraint constraintWithItem: _overlayView
                             attribute: NSLayoutAttributeBottom
                             relatedBy: NSLayoutRelationGreaterThanOrEqual
                                toItem:addTag
                             attribute:NSLayoutAttributeBottom
                            multiplier:1
                              constant:20]];

[addTag setTitle:@"Add Tag" forState:UIControlStateNormal];

我需要指定高度和寬度嗎? 有沒有辦法可以自動設置。

如果添加 [addTag sizeToFit],則左上角會顯示一個按鈕。

好吧...我花了一天半的時間。 但是我回答了我自己的問題。

我需要將對按鈕的自動轉換蒙版更改為約束,而不是視圖上的約束。

[addTag setTranslatesAutoresizingMaskIntoConstraints:NO];

代替

[_overlayView setTranslatesAutoresizingMaskIntoConstraints:NO];

斯威夫特 5

addTag.translatesAutoresizingMaskIntoConstraints = false

暫無
暫無

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

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