ios - UIButton 不显示在自定义 UIView 中

标签 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

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

最佳答案

“分解”你的FKPoiMiniDetail

FKPoiMiniDetail

View
buttonContainer1 - buttonContainer2 — buttonContainer3, etc

所以 buttonContainer 中的内容应该始终具有相同的“框架”,而 buttonContainer frame 会根据按钮的数量而变化,因为 buttonContainer 将是他们的 super View 。

所以你声明:

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

这将有助于设置 buttonContainer frame

但是 labeliv 不应该依赖于它们,只依赖于 [buttonContainer frame] 的高度和宽度,因为它们看起来居中。

我猜从你的代码来看,如果你在末尾执行 [self addSubview:iv][self addSubview:label]; 你可能会看到它们。

关于ios - UIButton 不显示在自定义 UIView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28632141/

相关文章:

ios - 强制 View Controller 重新加载以刷新 UIAppearance 更改

iOS iPhone 6 自动布局 - 全宽和全高

ios - 如何为不同的 iOS 版本定义代码

performance - CATiledLayer 用按钮绘制巨大的 UIView?

ios - UICollectionView 未正确返回单元格

ios - 开发插入与分销插入

ios - 如何以编程方式指定 Core Data fetchRequest 的目标?

ios - 如何强制异步保存常量子类?

javascript - shouldStartLoadWithRequest 未在我的 iPhone 应用程序中调用

objective-c - ObjC-UIViews : animating view and subview independently?