簡體   English   中英

UIImageView大小更改

[英]UIImageView size changes

我有帶有自定義視圖的導航欄。 UIImageView和UILabel是titleView的子視圖。 我將UITapGestureRecognizer添加到titleView,以使其在用戶點擊時顯示另一個UIViewController。 當您點擊titleView時,其他UIViewController將打開。 但是,當您單擊我的titleView中的后退按鈕時,UIImageView的大小會發生變化。 這是代碼

UIView *titleView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, self.view.frame.size.width, 30)];
UILabel *title = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, self.view.frame.size.width/2, 30)];
title.text = _groupName;
[title setTextColor:[self colorFromHexString:@"#474747"]];
[title setFont:[UIFont fontWithName:@"Roboto-Bold" size:16]];
[title setTextAlignment:NSTextAlignmentCenter];
[title setBackgroundColor:[UIColor clearColor]];
_imageView.frame = CGRectMake(0, 0, 30, 30);
_imageView.layer.cornerRadius = 15.0;
_imageView.layer.masksToBounds = YES;
_imageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
_imageView.layer.borderWidth = 0.1;
_imageView.contentMode = UIViewContentModeScaleAspectFit;
title.frame = CGRectMake(title.frame.origin.x+20, title.frame.origin.y, self.view.frame.size.width/2, 30);
UITapGestureRecognizer *recognizer;
recognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(titleTapped:)];
titleView.userInteractionEnabled = YES;
[titleView addGestureRecognizer:recognizer];
[titleView addSubview:title];
[titleView setBackgroundColor:[UIColor clearColor]];
[titleView addSubview:_imageView];
self.navigationItem.titleView = titleView;

這是您可以看到更改的圖像 導航欄

newViewController

回來后

我檢查您的代碼將剪輯添加到邊界,而無需兩次標題框架。

_imageView.frame = CGRectMake(0, 0, 30, 30);
_imageView.layer.cornerRadius = 15.0;
_imageView.layer.masksToBounds = YES;
_imageView.clipsToBounds = true;

與其直接設置視圖框架,不如嘗試使用自動布局 我懷疑正在發生的事情是您的自定義視圖在后退按鈕之后被重新布局,並且正在還原為圖像視圖的instrinsicSize

相反,您可以做的是關閉子視圖上的translatesAutoresizingMaskIntoConstraints ,然后添加約束來放置它們,最后向圖像視圖添加寬度和高度約束。

如果您打算國際化您的應用程序,也建議您使用自動版式-如果您正確設置了約束,則翻譯后的標題將適合並且您將不必再使用硬編碼的值。

暫無
暫無

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

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