UIButton不会出现在自定义UIView中

时间:2015-02-20 15:08:31

标签: ios objective-c uiview uibutton

我正在敲打这个但是无法弄清楚这个问题。

我有一个自定义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];

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

enter image description here

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

enter image description here

和第二个按钮的变量:

enter image description here

感谢任何帮助,提前谢谢!

2 个答案:

答案 0 :(得分:1)

"分解"你的FKPoiMiniDetail

FKPoiMiniDetail

  

查看
   buttonContainer1 - buttonContainer2 - buttonContainer3等

那么buttonContainer内的内容应始终具有相同的"帧"而buttonContainer frame根据按钮的数量而变化,因为buttonContainer将是他们的超级视图。

所以你声明:

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

帮助设置buttonContainer frame

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

我想你的代码,如果你在最后[self addSubview:iv][self addSubview:label];,你可能会看到它们。

答案 1 :(得分:0)

我在过去UIImageView设置图片时遇到问题,我的解决方案一直在改变订单:

[buttonContainer addSubview:iv]; [iv setImage:image];

首先添加子视图,然后设置图像。