ios - 以编程方式创建具有默认图标的导航项 IOS

标签 ios swift uinavigationbar

我想以编程方式使用默认的 XCode 刷新图标创建导航项“刷新”。我知道如何使用以下代码创建一个带有“刷新”一词的。但是,我希望有图标 insetead 的单词“刷新” 感谢您提前帮助

    let refreshButton = UIBarButtonItem(title: "Refresh", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(FollowVC.refresh))

refresh

最佳答案

绝妙的问题。我针对您的问题尝试了样本一。我找到了解决方案并且效果很好。

你在编码中犯的错误是

首先是你的

let refreshButton = UIBarButtonItem(title: "Refresh", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(FollowVC.refresh)) 

是错误的。你必须使用或编写

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

查看截图中的刷新按钮

enter image description here

好的解决方案

Add Default Icons to Navigation Bar

Navigation Bar Item Programmatically

Adding Navigation Bar item Programmatically

关于ios - 以编程方式创建具有默认图标的导航项 IOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38966238/

相关文章:

ios - 在调试器中出现错误 : This app has attempted to access privacy-sensitive data without a usage description

ios - 如何仅在函数加载时显示警报? | swift

iphone - 使用 UIAppearance 改变标签高度

iphone - iOS7 中的导航栏颜色不同?

ios - Xamarin iOS : navigation bar not showing after pushing view

ios - iOs 8 上的 Twitter API 有什么问题?

iOS Core 蓝牙状态保存和恢复问题

android - Flutter:错误:找不到 Getter: 'suspending' 。案例 AppLifecycleState.suspending

c - 从 C 到 Swift 的函数回调

swift 2 : understanding AnyObject and Self