Change position of UIBarButtonItem in UINavigationBar

后端 未结 20 1550
一个人的身影
一个人的身影 2020-11-27 10:31

How can I change the position of a UIBarButtonItem in a UINavigationBar? I would like my button to be about 5px higher than its normal position.

20条回答
  •  野性不改
    2020-11-27 11:01

    You can always do adjustments using Insets on the button. For example,

    UIButton *toggleBtn =  [UIButton buttonWithType:UIButtonTypeCustom];
    [toggleBtn setFrame:CGRectMake(0, 0, 20, 20)];
    [toggleBtn addTarget:self action:@selector(toggleView) forControlEvents:UIControlEventTouchUpInside];
    
    [toggleBtn setImageEdgeInsets:((IS_IPAD)? UIEdgeInsetsMake(0,-18, 0, 6) : UIEdgeInsetsMake(0, -3, 0, -3))];
    
    UIBarButtonItem *toggleBtnItem = [[UIBarButtonItem alloc] initWithCustomView: toggleBtn];
    self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:searchBtnItem, toggleBtnItem, nil];
    

    It works for me.

提交回复
热议问题