【问题标题】:Position UIBarbuttonItem to the VERY left将 UIBarbuttonItem 定位到最左边
【发布时间】:2012-11-13 06:44:42
【问题描述】:

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

我也尝试过更改 imageInsetUIButton

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

谢谢

【问题讨论】:

    标签: objective-c ios uibutton uitoolbar uitoolbaritem


    【解决方案1】:

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

        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];
    

    【讨论】:

    • UIView 仍然没有到达最左边,所以即使我移动按钮,触摸也不在 UIView 内部,因此不会触发触摸事件。
    • 你能发布任何视觉信息吗?它必须是什么样子?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-13
    • 2012-02-23
    • 2012-09-02
    • 2012-05-21
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    相关资源
    最近更新 更多