0

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

我也试过imageInset改变UIButton

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按钮的 不会影响按钮和左侧之间的间隙

谢谢

4

1 回答 1

0

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

    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];
于 2012-11-13T06:51:37.863 回答