[英]Constraint a UIBarButtonItem's size in the navigaiton bar with iOS 11
[英]iOS 11 UIBarButtonItem ImageView size bug
您好,今天早上在iOS 11上,我發現了很多iOS 10所沒有的錯誤。我糾正了除兩個錯誤以外的大多數錯誤,此錯誤位於右上方的配置文件圖像中
代碼按鈕:
UIImageView* v = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 35, 35)];
v.image = img;
v.layer.masksToBounds = YES;
v.layer.cornerRadius = 17.5;
UIBarButtonItem* rightBtn = [[UIBarButtonItem alloc] initWithCustomView:v];
self.navigationItem.rightBarButtonItem = rightBtn;
///// iOS 10 /////
///// iOS 11 /////
在iOS 11中,您必須需要我們自動布局而不是框架
像下面
myButton.widthAnchor.constraint(equalToConstant: customViewButton.width).isActive = true
myButton.heightAnchor.constraint(equalToConstant: customViewButton.height).isActive = true
我用以下代碼修復了這個問題:
[v.widthAnchor constraintEqualToConstant:35].active = YES;
[v.heightAnchor constraintEqualToConstant:35].active = YES;
我使用UIbutton修復了此問題。
@victor bill的答案就像一個咒語,但是我想提一下,如果您不先設置按鈕的框架,這將不起作用。 僅設置寬度和高度錨點會使按鈕在我的應用程序中完全消失。
v = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35, 35)];
[v.widthAnchor constraintEqualToConstant:35].active = YES;
[v.heightAnchor constraintEqualToConstant:35].active = YES;
如果您通常想為圖像/按鈕/ UIView設置固定大小,則這是Swift 4代碼:
yourButton.widthAnchor.constraint(equalToConstant: 100.0).isActive = true
yourButton.heightAnchor.constraint(equalToConstant: 30.0).isActive = true
您必須使用兩個約束手動設置寬度和高度,並將其設置為活動狀態。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.