我对此表示怀疑,但无法找出问题所在。

我有一个自定义UIView:FKPoiMiniDetail,这是实现(FKPoiMiniDetail.m)

const static CGFloat kDialogLateralMargin   = 15.0;
const static CGFloat kDialogHeight          = 100.0;
const static CGFloat kDialogBottomMargin    = 10.0;
const static CGFloat kBottomButtonHeight    = 50.0;

@interface FKPoiMiniDetail ()
@property (nonatomic) float tabBarHeight;
@property (nonatomic) NSUInteger numberOfButtons;
@property (nonatomic) NSMutableArray *buttonBlocks;
@property (strong, nonatomic) PoI* myPoi;

@end
@implementation FKPoiMiniDetail

- (id) initWithTabBarHeight:(float)aTabBarHeight numberOfButtons:(NSUInteger)numButtons andPoi:(PoI*)aPoi{
    if (self = [super init]) {
        self.tabBarHeight = aTabBarHeight;
        self.numberOfButtons = numButtons;
        self.myPoi = aPoi;
        self.buttonBlocks = [[NSMutableArray alloc] init];
        [self configure];
    }
    return self;
}

在这里我以编程方式添加按钮:
- (void) assignButton:(NSUInteger)index anImage:(UIImage*)image aText:(NSString*)text andCallback:(void(^)())cb{
    if (index > self.numberOfButtons) {
        ALog("ERROR: Index is greater than number of buttons");
        return;
    }
    [self.buttonBlocks addObject:cb];

    int imWidth = 20;
    int imHeight = 20;
    int imLabelSpacing = 8;
    CGSize labelSize = [text sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17.0f]}];
    CGFloat overallWidth = labelSize.width + imWidth + imLabelSpacing;

    CGFloat test = self.bounds.size.width;
    CGFloat buttonWidth = test / ((CGFloat) self.numberOfButtons);

    int x = index * buttonWidth;

    int y = self.frame.size.height - kBottomButtonHeight;

    UIButton *buttonContainer = [[UIButton alloc] initWithFrame:CGRectMake(x,y,buttonWidth,kBottomButtonHeight)];
    [buttonContainer addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
    buttonContainer.tag = index;


    int firstImOrigin = x + (buttonWidth - overallWidth) / 2;
    UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(firstImOrigin, (kBottomButtonHeight - imHeight)/2, imWidth, imHeight)];
    [iv setImage:image];
    [buttonContainer addSubview:iv];


    CGRect lR = CGRectMake(firstImOrigin + imWidth + imLabelSpacing, (kBottomButtonHeight - labelSize.height)/2, labelSize.width, labelSize.height);

    UILabel *label = [[UILabel alloc] initWithFrame:lR];
    label.text = text;

    [buttonContainer addSubview:label];
    [self addSubview:buttonContainer];
}

这是作为UIButton目标的选择器:
- (void)buttonTouched:(UIButton*)b{
    void (^ myblock)() = [self.buttonBlocks objectAtIndex:b.tag];
    myblock();
}

我在其他地方创建此对象并分配如下按钮:
FKPoiMiniDetail *md = [[FKPoiMiniDetail alloc] initWithTabBarHeight:self.tabBarController.tabBar.frame.size.height
                                                            numberOfButtons:2
                                                                     andPoi:annotation.aPoI];
UIImage *im = [UIImage imageNamed:@"detail.png"];
[md assignButton:0 anImage:im aText:@"Dettagli" andCallback:^{
     ALog("Button 0 pressed");
}];

[md assignButton:1 anImage:im aText:@"Naviga" andCallback:^{
     ALog("Button 1 pressed");
}];
[self.mapView addSubview:md];

第二个按钮是空的,但是奇怪的是:在按钮内部单击,回调确实执行了...

调试似乎没用:
第一个按钮的变量:

以及第二个按钮的变量:

任何帮助表示赞赏,在此先感谢!

最佳答案

“分解”您的FKPoiMiniDetailFKPoiMiniDetail
视图
buttonContainer1-buttonContainer2-buttonContainer3,依此类推

因此,buttonContainer内的内容应始终具有相同的“框架”,而buttonContainer frame会根据按钮的数量而变化,因为buttonContainer将是其 super 视图。

所以你声明:

int x = index * buttonWidth;
int y = self.frame.size.height - kBottomButtonHeight;

这将帮助设置buttonContainer frame

但是labeliv不应该依赖于它们,而仅取决于[buttonContainer frame]的高度和宽度,因为它们似乎居中。

我想从您的代码来看,如果您在[self addSubview:iv][self addSubview:label];末尾进行操作,则可能会看到它们。

10-08 05:27