ios - 将自定义图像 UIButtons 添加到自定义 UIToolbar 无法正常工作

标签 ios uibutton uiimage uitoolbar programmatically-created

我已经实现了一个带有 4 个 UIbutton 的自定义 UIToolBar。这些按钮是带有图像的按钮。我用这段代码创建了它们:

-(NSArray *)setButtonForToolBar
{
    CGRect buttonFrame = {
        .origin.x = 0,
        .origin.y = 0,
        .size.width = self.frame.size.width/4,
        .size.height = self.frame.size.height,
    };
    
    NSLog(@"Frame width: %f  and Origin x: %f ", buttonFrame.size.width, buttonFrame.origin.x );
    // BOTON CHECK IN
    UIButton *checkinButton = [[UIButton alloc]initWithFrame:buttonFrame] ;
    [checkinButton addTarget:self action:@selector(checkInService:) forControlEvents:UIControlEventTouchUpInside];
    [checkinButton setImage:[UIImage imageNamed:@"btn-checkin.png"] forState:UIControlStateNormal];
    [checkinButton setImage:[UIImage imageNamed:@"btn-checkin-selected.png"] forState:UIControlStateHighlighted | UIControlStateSelected];
    checkinButton.tag = 0;
    UIBarButtonItem *checkInBarBtn = [[UIBarButtonItem alloc] initWithCustomView: checkinButton];
    

    // BOTON PARA LA INFO
    buttonFrame.origin.x = CGRectGetMaxX(buttonFrame);
NSLog(@"Frame width: %f  and Origin x: %f ", buttonFrame.size.width, buttonFrame.origin.x );
    UIButton *infoBtn = [[UIButton alloc]initWithFrame:buttonFrame];
    [infoBtn addTarget:self action:@selector(checkInService:) forControlEvents:UIControlEventTouchUpInside];
    [infoBtn setImage:[UIImage imageNamed:@"btn-info.png"] forState:UIControlStateNormal];
    [infoBtn setImage:[UIImage imageNamed:@"btn-info-selected.png"] forState:UIControlStateHighlighted | UIControlStateSelected];
    infoBtn.tag = 1;
    
    UIBarButtonItem *infoBarBtn = [[UIBarButtonItem alloc] initWithCustomView: infoBtn];
    
    
    // BOTON PARA VALORACIONES
     buttonFrame.origin.x = CGRectGetMaxX(buttonFrame);
  NSLog(@"Frame width: %f  and Origin x: %f ", buttonFrame.size.width, buttonFrame.origin.x );
    UIButton *voteBtn = [[UIButton alloc]initWithFrame:buttonFrame];
    [voteBtn addTarget:self action:@selector(checkInService:) forControlEvents:UIControlEventTouchUpInside];
    [voteBtn setImage:[UIImage imageNamed:@"btn-comment.png"] forState:UIControlStateNormal];
    [voteBtn setImage:[UIImage imageNamed:@"btn-comment-selected.png"] forState:UIControlStateHighlighted];
    voteBtn.tag = 2;
    UIBarButtonItem *voteBarBtn = [[UIBarButtonItem alloc] initWithCustomView: voteBtn];
    
    
    // BOTON PARA PLANES
     buttonFrame.origin.x = CGRectGetMaxX(buttonFrame);
           NSLog(@"Frame width: %f  and Origin x: %f ", buttonFrame.size.width, buttonFrame.origin.x );
    UIButton *planBtn = [[UIButton alloc]initWithFrame:buttonFrame];
    [planBtn addTarget:self action:@selector(checkInService:) forControlEvents:UIControlEventTouchUpInside];
    [planBtn setImage:[UIImage imageNamed:@"btn-fav.png"] forState:UIControlStateNormal];
    [planBtn setImage:[UIImage imageNamed:@"btn-fav-selected.png"] forState:UIControlStateHighlighted | UIControlStateSelected];
    planBtn.tag = 3;
    
    UIBarButtonItem *planBarBtn = [[UIBarButtonItem alloc] initWithCustomView: planBtn];
    
    
    
    
    NSArray *buttonItems = [NSArray arrayWithObjects:checkInBarBtn, infoBarBtn, voteBarBtn, planBarBtn, nil];
    return buttonItems;
}

ToolBar 是用 = (10,400,300,60) 的框架创建的。

查看 NSlog 输出:

2013-11-21 17:43:18.557 Woowplanet[3316:70b] Frame width: 75.000000  and Origin x: 0.000000 
2013-11-21 17:43:18.569 Woowplanet[3316:70b] Frame width: 75.000000  and Origin x: 75.000000 
2013-11-21 17:43:18.570 Woowplanet[3316:70b] Frame width: 75.000000  and Origin x: 150.000000 
2013-11-21 17:43:18.571 Woowplanet[3316:70b] Frame width: 75.000000  and Origin x: 225.000000 

输出显示它们应该在正确的位置,我不知道为什么它们在右边将近 10 磅..

问题是按钮不在正确的位置。看图enter image description here

谢谢。

最佳答案

试试这个:

创建具有灵活空间的条形按钮

 UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

然后这样做:

NSArray *buttonItems = [NSArray arrayWithObjects:flexibleSpace,checkInBarBtn,flexibleSpace, infoBarBtn,flexibleSpace, voteBarBtn,flexibleSpace, planBarBtn,flexibleSpace, nil];

关于ios - 将自定义图像 UIButtons 添加到自定义 UIToolbar 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20127037/

相关文章:

iphone - 在没有淡入淡出效果的情况下禁用 UIButton

ios - swift 3 : How to give option to choose the phone number to call

objective-c - 构建具有多个可拉伸(stretch)图像的 UIImage

ios - 无法加载从带有标识符的包中的 nib 引用的图像

ios - 我是否必须添加我在 ios8 应用程序中使用的所有框架?

ios - 应用程序是否可以获取用户使用另一个应用程序的时间信息?

iPhone 4 "device not registered"...需要帮助备份一些应用程序数据

ios - 何时在 iOS 应用程序中使用 sqlite3_close

ios - 应用程序显示启动画面然后关闭

iOS - 如何按顺序执行两个转换?