2015-09-10 48 views
0

我該如何減少兩個項目之間的距離? 刪除UITabBarItem下面的空格?

我的代碼:

-(void)setupRightMenuButton{ 
    filtro = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon-filtro-bar"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonFilter)]; 
    busca = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon-busca"] style:UIBarButtonItemStylePlain target:self action:@selector(moveToSearchView)]; 

    filtro.tintColor = [UIColor whiteColor]; 
    busca.tintColor = [UIColor whiteColor]; 

    filtro.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0); 
    busca.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0); 

    [self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:busca, filtro, nil] animated:YES]; 
} 

我viewDidLoad中:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [self setupLeftMenuButton]; 

    [self.navigationController.navigationBar setBarTintColor: [UIColor colorWithRed:0.49 green:0.075 blue:0.082 alpha:1]]; /*#7d1315*/ 
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}]; 
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor]; 

    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) 
                 forBarMetrics:UIBarMetricsDefault]; 
    [self setupRightMenuButton]; 
    self.title = @"Músicas"; 

} 

我嘗試使用filtro.imageInsets = UIEdgeInsetsMake (6, 0, -6, 0);,但它沒有工作

+0

你加入這兩個之間的間隔? –

+0

我沒有把空間 –

+0

嘗試添加按鈕到導航欄,而不是做「setRightBarButtonItems」 –

回答

0

嘗試添加按鈕這樣:

UIButton *but1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, width, height)]; 
    UIView *but1View = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)]; 
    [but1View addSubview:but1]; 
    UIBarButtonItem *rightButton1 = [[UIBarButtonItem alloc] initWithCustomView:but1View]; 

    UIButton *but2 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, width, height)]; 
    UIView *but2View = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)]; 
    [but2View addSubview:but2]; 
    UIBarButtonItem *rightButton2 = [[UIBarButtonItem alloc] initWithCustomView:but2View]; 

     [email protected][ rightButton,rightButton2]; 

您需要提及兩個UIViews的x和y的位置,因爲您希望您的按鈕位於導航欄上。

+0

你可以將圖像添加到按鈕。我沒有提到,在回答 –

+0

必要的按鈕仍然是我使用的。那麼解決方案將無法正常工作 –

+0

抱歉,我沒有得到你所說的。你的意思是什麼按鈕仍然存在? –

0

此代碼適用於我。嘗試創建一個按鈕,然後將圖像設置爲它並設置按鈕框。

let button: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton 
let image = UIImage(named: "your_image")! 
button.setImage(image , forState: UIControlState.Normal) 
// optional ------------- 
button.addTarget(self, action: "your_function", forControlEvents: UIControlEvents.TouchUpInside) 
// ------------ 
button.frame = CGRectMake(0, 0, image.width, image.height) 
let barButton = UIBarButtonItem(customView: button) 

,只要你想創建許多barButtons然後將它們添加到navigationItem:

self.navigationItem.setRightBarButtonItems([barBtn1, barBtn2], animated: false)