以编程方式创建具有默认图标的导航项 IOS
Create navigation item with default Icon programatically IOS
我想以编程方式创建具有默认 XCode 刷新图标的导航项 "Refresh"。我知道如何用下面的代码用 "Refresh" 这个词创建一个。但是,我想要的是单词的图标插入"Refresh"感谢您提前提供帮助
let refreshButton = UIBarButtonItem(title: "Refresh", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(FollowVC.refresh))
您应该使用 .Refresh
而不是 .Plain
。
let refreshButton = UIBarButtonItem(barButtonSystemItem: .Refresh, target: self, action: #selector(FollowVC.refresh))
此外,您应该使用 init(barButtonSystemItem:target:action:)
而不是 init(title:style:target:action:)
。
太棒了 question.I 为您尝试了示例一 question.I 得到了解决方案并且工作正常。
你的编码错误是
首先是你的
let refreshButton = UIBarButtonItem(title: "Refresh", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(FollowVC.refresh))
是wrong.You必须使用或写
UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem, target: AnyObject?, action:Selector)
其次你使用了UIBarButtonItemStyle.Plain。如果你想要刷新按钮,你必须设置UIBarButtonSystemItem.Refresh。
SWIFT
override func viewDidLoad()
{
// Create the navigation bar
let navigationBar = UINavigationBar(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: self.view.frame.size.width, height: 64))) // set frame here
//set the background color
navigationBar.backgroundColor = UIColor.white
navigationBar.delegate = self as? UINavigationBarDelegate;
// Create a navigation item with a title
let navigationItem = UINavigationItem()
navigationItem.title = "title" //If you want to set a tilte set here.Whatever you want set here.
// Create button for navigation item with refresh
let refreshButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.refresh, target: self, action: Selector(("actionRefresh:")))
// I set refreshButton for the navigation item
navigationItem.leftBarButtonItem = refreshButton
// Assign the navigation item to the navigation bar
navigationBar.items = [navigationItem]
// Make the navigation bar a subview of the current view controller
self.view.addSubview(navigationBar)
}
#pragma action method
func actionRefresh(sender: UIBarButtonItem)
{
print("The action button is refreshed")
}
打印语句为
The action button is refreshed
OBJECTIVE C
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
navbar.backgroundColor = [UIColor whiteColor];
navbar.delegate = self;
UINavigationItem * navItem = [[UINavigationItem alloc] init];
navItem.title = @"Title"; //Set Title Here Wahtever You Want Here
UIBarButtonItem *buttonRefresh = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self
action:@selector(actionRefresh:)];
navItem.leftBarButtonItem = buttonRefresh;
navbar.items = @[navItem];
[self.view addSubview:navbar];
}
NSLog语句是
The button action refresh is performed
查看屏幕截图中的刷新按钮
好的解决方案
Navigation Bar Item Programmatically
Adding Navigation Bar item Programmatically
我想以编程方式创建具有默认 XCode 刷新图标的导航项 "Refresh"。我知道如何用下面的代码用 "Refresh" 这个词创建一个。但是,我想要的是单词的图标插入"Refresh"感谢您提前提供帮助
let refreshButton = UIBarButtonItem(title: "Refresh", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(FollowVC.refresh))
您应该使用 .Refresh
而不是 .Plain
。
let refreshButton = UIBarButtonItem(barButtonSystemItem: .Refresh, target: self, action: #selector(FollowVC.refresh))
此外,您应该使用 init(barButtonSystemItem:target:action:)
而不是 init(title:style:target:action:)
。
太棒了 question.I 为您尝试了示例一 question.I 得到了解决方案并且工作正常。
你的编码错误是
首先是你的
let refreshButton = UIBarButtonItem(title: "Refresh", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(FollowVC.refresh))
是wrong.You必须使用或写
UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem, target: AnyObject?, action:Selector)
其次你使用了UIBarButtonItemStyle.Plain。如果你想要刷新按钮,你必须设置UIBarButtonSystemItem.Refresh。
SWIFT
override func viewDidLoad()
{
// Create the navigation bar
let navigationBar = UINavigationBar(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: self.view.frame.size.width, height: 64))) // set frame here
//set the background color
navigationBar.backgroundColor = UIColor.white
navigationBar.delegate = self as? UINavigationBarDelegate;
// Create a navigation item with a title
let navigationItem = UINavigationItem()
navigationItem.title = "title" //If you want to set a tilte set here.Whatever you want set here.
// Create button for navigation item with refresh
let refreshButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.refresh, target: self, action: Selector(("actionRefresh:")))
// I set refreshButton for the navigation item
navigationItem.leftBarButtonItem = refreshButton
// Assign the navigation item to the navigation bar
navigationBar.items = [navigationItem]
// Make the navigation bar a subview of the current view controller
self.view.addSubview(navigationBar)
}
#pragma action method
func actionRefresh(sender: UIBarButtonItem)
{
print("The action button is refreshed")
}
打印语句为
The action button is refreshed
OBJECTIVE C
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
navbar.backgroundColor = [UIColor whiteColor];
navbar.delegate = self;
UINavigationItem * navItem = [[UINavigationItem alloc] init];
navItem.title = @"Title"; //Set Title Here Wahtever You Want Here
UIBarButtonItem *buttonRefresh = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self
action:@selector(actionRefresh:)];
navItem.leftBarButtonItem = buttonRefresh;
navbar.items = @[navItem];
[self.view addSubview:navbar];
}
NSLog语句是
The button action refresh is performed
查看屏幕截图中的刷新按钮
好的解决方案
Navigation Bar Item Programmatically
Adding Navigation Bar item Programmatically