调整navigationBar按钮的位置

按照一般的方法创建导航栏的左右按钮时,会出现按钮向里偏的现象,但有时想让按钮往屏幕边靠怎么办呢?


1.首先先创建按钮


    UIButton *button = [[UIButton alloc] init];
    [button setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:selectedImageName] forState:UIControlStateHighlighted];
    button.size = button.currentBackgroundImage.size;
    [button addTarget:self action:@selector(ABC) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barBtn  = [[UIBarButtonItem alloc] initWithCustomView:button];

2.创建个UIBarButtonSystemItemFixedSpace的UIBarButtonItem,并设置间距

    UIBarButtonItem *navigationSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    navigationSpacer.width = -10;//设置间距

3.设置导航栏的按钮群

self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:navigationSpacer,BarButton, nil];
此时你设置的右按钮就会往右偏移了。设置左按钮也以此类推。

你可能感兴趣的:(Navigation)