How to add barbutton programmatically with image

后端 未结 4 477
温柔的废话
温柔的废话 2021-01-24 11:47

I am creating a barbutton programmatically. But it can\'t fix into screen. Help me in solve this problem.

Screenshot:

4条回答
  •  Happy的楠姐
    2021-01-24 12:31

    UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [myButton setImage:[UIImage imageNamed:@"request-1.png"] forState:UIControlStateNormal];
    
    myButton.frame = CGRectMake(0, 0, 80, 36);
    
    [myButton addTarget:self action:@selector(requestButton) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * aBarButton = [[[UIBarButtonItem alloc] initWithCustomView:myButton] autorelease];
    
    
    self.navigationItem.rightBarButtonItem = aBarButton;
    
    
    
    [myButton release];
    

提交回复
热议问题