iOS11導覽列leftBarButtonItem顯示失常

來源:互聯網
上載者:User

標籤:rect   .so   跳轉   app   events   err   touch   sel   space   

如果按照系統內建的方法寫沒有問題,代碼如下:

UIBarButtonItem* leftItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage  imageNamed:@"default"]

imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]

style: UIBarButtonItemStylePlaintarget:self action:@selector(leftItemAction)];

self.navigationItem.leftBarButtonItem= leftItem;

但是顯示的圖片是使用者的頭像,是一個隨時可能修改的url連結,這時候系統內建的方法就無法滿足了,

必須使用自訂,代碼如下:

//_personalBtn為當前控制器屬性值,因為使用者修改映像的時候需要重新整理(需要用到第三方庫SDWebImage)

_personalBtn= [UIButtonbuttonWithType:UIButtonTypeCustom];

_personalBtn.frame=CGRectMake(0,0,40,40);

[_personalBtn sd_setImageWithURL:[NSURL URLWithString:_model.headUrl]

forState:UIControlStateNormal placeholderImage:[UIImageimageNamed:@"ev_circle_default"]];

_personalBtn.layer.masksToBounds=YES;

_personalBtn.layer.cornerRadius=40/2;

[_personalBtn addTarget:selfaction:@selector(leftItemAction) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem* leftItem = [[UIBarButtonItem  alloc]  initWithCustomView:_personalBtn];

self.navigationItem.leftBarButtonItem= leftItem;

顯示如下: 

 

 

現在顯示正常,可能因為有這句代碼_personalBtn.frame=CGRectMake(0,0,40,40);

當你跳轉其它頁面在返回回來的時候就顯示如下了:

連標題都不見了,是不是很奇葩,換成UIImageView也是如此。

解決代碼:

  UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0,40,40)];

    view.backgroundColor = [UIColor clearColor];

    

    _personalBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    _personalBtn.frame = CGRectMake(0, 0, 40, 40);

    [_personalBtn sd_setImageWithURL:[NSURL URLWithString:_model.headUrl] forState:UIControlStateNormal

  placeholderImage:[UIImage imageNamed:@"ev_circle_default"]];

    _personalBtn.layer.masksToBounds = YES;

    _personalBtn.layer.cornerRadius = 40/2;

    [_personalBtn addTarget:self action:@selector(leftItemAction) forControlEvents:UIControlEventTouchUpInside];

    [view addSubview:_personalBtn];

 

    UIBarButtonItem * leftItem = [[UIBarButtonItem alloc] initWithCustomView:view];

 self.navigationItem.leftBarButtonItem= leftItem;

 至於為什麼解決了,我也想不明白,可能是view的大小限制了btn的大小,謝謝瀏覽,如果你找到了這其中的原因,求賜教

iOS11導覽列leftBarButtonItem顯示失常

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.