UIBarButtonItem with custom image and no border

前端 未结 9 692
旧巷少年郎
旧巷少年郎 2020-12-12 12:18

I want to create a UIBarButtonItem with a custom image, but I don\'t want the border that iPhone adds, as my Image has a special border.

It\'s the same as the back b

9条回答
  •  半阙折子戏
    2020-12-12 12:56

    This can also be done programmatically as well (of-course):

    First, create a custom view. This custom view can contain an image, button or whatever else you would like. The custom view can be made programmatically or in IB:

    UIImage *customImage = [UIImage imageNamed:@"imageName"];
    UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, customImage.size.width, customImage.size.height)];
    customView.backgroundColor = [UIColor colorWithPatternImage:customImage];
    

    Next, create a UIBarButtonItem and initialize it with the custom view.

    UIBarButtonItem *customBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:customView];
    

    Now, just add the custom UIBarButton to the leftBarButtonItem:

    self.navigationItem.leftBarButtonItem = customBarButtonItem;
    

提交回复
热议问题