修改navigationbar右侧按钮离屏幕边缘位置

来源:互联网 发布:adobe pdf 电脑软件 编辑:程序博客网 时间:2024/05/16 18:07

先上代码

UIButton *settingBtn = [Utils creatCustomButtonWithFrame:CGRectMake(0, 0, 60, 40) btnTitle:@"设置" btnFontSize:14 titleColor:[UIColor blackColor]];[settingBtn addTarget:self action:@selector(clickTheSettingBtn) forControlEvents:UIControlEventTouchUpInside];UIBarButtonItem *rightBar = [[UIBarButtonItem alloc]initWithCustomView:settingBtn];self.navigationItem.rightBarButtonItem = rightBar;

效果为

这里写图片描述

若想调节该按钮位置,设置

settingBtn.titleLabel.textAlignment = NSTextAlignmentRight;

这样是没效果的。应利用UIBarButtonSystemItemFixedSpace格式,rightbar设为数组,进行填充。

UIButton *settingBtn = [SHUtils creatCustomButtonWithFrame:CGRectMake(0, 0, 60, 40) btnTitle:@"设置" btnFontSize:14 titleColor:[UIColor blackColor]];[settingBtn addTarget:self action:@selector(clickTheSettingBtn) forControlEvents:UIControlEventTouchUpInside];UIBarButtonItem *rightBar = [[UIBarButtonItem alloc]initWithCustomView:settingBtn];UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]                                   initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace                                   target:nil action:nil];negativeSpacer.width = -20;self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:negativeSpacer,rightBar,nil];

效果为

这里写图片描述

原文地址:http://www.cnblogs.com/Apologize/p/5764852.html

原创粉丝点击