将uibarbuttonitem放在左侧



我有一个我想拥抱屏幕左侧的UIBarButtonItem。我尝试在按钮和其他配置之前将固定的barbutton放置在 width = 0 之前,但是按钮和 x = 0 之间总是有几个像素。

我还尝试更改UIButtonimageInset

UIButton *moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
moreButton.frame = CGRectMake(0, 0, 60, 35);
[moreButton setImage:[UIImage imageNamed:@"Morebutton"] forState:UIControlStateNormal];
moreButton.imageEdgeInsets = UIEdgeInsetsMake(0, -8, 0, 8);
[moreButton addTarget:self action:@selector(pressedMoreButton:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *moreBar = [[[UIBarButtonItem alloc] initWithCustomView:moreButton] autorelease];

但是在屏幕边缘附近敲击并不会触发selector(可能是因为仅移动图像,但不是按钮本身),并且增加按钮的size不会影响按钮之间的差距左侧。

谢谢

尝试将您的按钮放在容器视图上,并在视图上更改按钮的位置。

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:buttonImage forState:UIControlStateNormal];   
    button.contentMode = UIViewContentModeScaleAspectFit;       
    button.frame= CGRectMake(-4.0, 0.0, 30, 30);
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    UIView *v=[[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, cellTextSize.width+10, buttonImage.size.height)] autorelease];
    v.backgroundColor = [UIColor clearColor];
    [v addSubview:button];
    return [[[UIBarButtonItem alloc] initWithCustomView:v] autorelease];

最新更新