iOS 設(shè)置導(dǎo)航按鈕的默認偏移量問題

以前導(dǎo)航的做按鈕或者右按鈕一直沒注意一些細節(jié)問題,比如下圖:

pic1.png

但是新出的圖是這樣的:

pic2.png

右邊完全是沒空隙的,緊貼著,而且圖片變大,點擊的也變大
因此,查了一下網(wǎng)上的方法,在此記錄一下:

左邊的方法為:
    UIButton *backButton =[UIButton  buttonWithType:UIButtonTypeCustom];
    [backButton setBackgroundImage:[UIImage imageNamed:imageNamed] forState:UIControlStateNormal];
    backButton.frame=CGRectMake(0, 0, 44, 44);
    [backButton addTarget:self action:@selector(leftAction) forControlEvents:(UIControlEventTouchUpInside)];
    UIBarButtonItem*item=[[UIBarButtonItem alloc]initWithCustomView:backButton];
    UIBarButtonItem *navSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    navSpace.width = - 15;
    self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:navSpace,item, nil];
右邊的方法為:
    UIButton *addsearchForTalentButton =[UIButton  buttonWithType:UIButtonTypeCustom];
    [addsearchForTalentButton setBackgroundImage:[UIImage imageNamed:@"search_talents_icon_search"] forState:UIControlStateNormal];
    addsearchForTalentButton.frame=CGRectMake(0, 0, 44, 44);
    [addsearchForTalentButton addTarget:self action:@selector(addsearchForTalentButtonClick) forControlEvents:(UIControlEventTouchUpInside)];   
    UIBarButtonItem*item=[[UIBarButtonItem alloc]initWithCustomView:addsearchForTalentButton];
    UIBarButtonItem *navSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    navSpace.width = - 15;
    self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:navSpace,item, nil];

最后實現(xiàn)的結(jié)果如圖所示:

pic3.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容