2016-08-16 12 views
2

デフォルトのXCodeリフレッシュアイコンを使用してナビゲーション項目 "更新"をプログラムで作成します。私は、次のコードで "Refresh"という単語を作成する方法を知っています。しかし、私は言葉事前デフォルトでアイコンをプログラムで作成IOS

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

refresh

+0

良い質問は既に尋ねられました。 – user3182143

+0

私の更新された回答は、目的のcとswiftの両方をチェックしてください。 – user3182143

答えて

5

ご質問のサンプルを試しました。私は解決策を得ており、うまく動作します。あなたのコーディングでミスを犯したものを

まずにあるあなたの

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

wrong.Youを使用するか、UIBarButtonItemStyle.Plainを使用

UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem, target: AnyObject?, action:Selector) 

セカンドを記述する必要があります。リフレッシュボタンが必要な場合は、UIBarButtonSystemItem.Refreshを設定する必要があります。

SWIFT

override func viewDidLoad() 
{ 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

    // Create the navigation bar 
    let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 64)) // set frame here 

    //set the background color   
    navigationBar.backgroundColor = UIColor.whiteColor() 
    navigationBar.delegate = self; 

    // 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: "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") 
} 

Printステートメントが

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

のスクリーンショットの更新ボタンを参照してください。
+0

詳細なソリューションをありがとう!素晴らしい作品 – user172902

+0

ありがとう – user3182143

1

代わりの.Plainを使用して助けてくれてありがとう、あなたは.Refreshを使用しなければならない「更新」のアイコンinseteadを持っていると思います。

let refreshButton = UIBarButtonItem(barButtonSystemItem: .Refresh, target: self, action: #selector(FollowVC.refresh))

また、あなたの代わりにinit(title:style:target:action:)init(barButtonSystemItem:target:action:)を使用する必要があります。

関連する問題