iPhone: Adding Info button as right bar button item in navigation bar in code

前端 未结 4 433
北海茫月
北海茫月 2021-02-04 06:42

Has anyone had any success creating an info button (italic \'i\' in a circle) in code (read: without Interface Builder), and then assigning it as the right bar button item of a

4条回答
  •  旧时难觅i
    2021-02-04 07:28

    I guess it will be more complete response and it should help in any situation:

    -(void)viewDidLoad{
    
        //Set Navigation Bar
        UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
    
        //Set title if needed
        UINavigationItem * navTitle = [[UINavigationItem alloc] init];
        navTitle.title = @"Title";
    
        //Here you create info button and customize it
        UIButton * tempButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
    
        //Add selector to info button
        [tempButton addTarget:self action:@selector(infoButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    
        UIBarButtonItem * infoButton = [[UIBarButtonItem alloc] initWithCustomView:tempButton];
    
        //In this case your button will be on the right side
        navTitle.rightBarButtonItem = infoButton;
    
        //Add NavigationBar on main view
        navBar.items = @[navTitle];
        [self.view addSubview:navBar];
    
    }
    

提交回复
热议问题