0

我有導航欄與自定義視圖。 UIImageView和UILabel是titleView的子視圖。我添加UITapGestureRecognizer到titleView,讓用戶點擊它時顯示另一個UIViewController。當你點擊titleView時,其他UIViewController打開。但是當你在我的titleView大小的UIImageView中點擊後退按鈕更改。下面是代碼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; 

在這裏,在那裏你可以看到的變化 navigationBar

newViewController

after back

回答

0

我檢查你的代碼添加剪輯邊界和不需要的圖像標題框架兩次。

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

,而不是直接設置的意見的框架,你可以嘗試使用Auto Layout;我懷疑發生了什麼事是您的自定義視圖會在後退按鈕後重新佈局,並且它將恢復爲圖像視圖的instrinsicSize

您可以改爲在子視圖上關閉translatesAutoresizingMaskIntoConstraints,然後添加約束來定位它們,最後將寬度和高度約束添加到圖像視圖。

如果您打算將您的應用程序國際化,也建議使用自動佈局 - 如果您正確設置了約束條件,那麼翻譯後的標題就會合適,並且您不必隨身攜帶硬編碼值。