1

我有一个习惯UIView,我可以在不同地方的整个应用程序中使用它。它基本上看起来像这样:

@interface BottomBar : UIView
{
    UIImageView *imageView;
    UILabel *nameLabel;
    UIButton *playPauseButton;
}
@end

@implementation BottomBar

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:CGRectMake(0.0, 524.0, 320.0, 44.0)];
    if (self) {
        imageView = [[UIImageView alloc] initWithFrame:CGRectMake(15.0, 2.0, 40.0, 40.0)];
        nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(70.0, 2.0, 180.0, 40.0)];
        playPauseButton = [[UIButton alloc] initWithFrame:CGRectMake(265.0, 2.0, 40.0, 40.0)];

        [self setBackgroundColor:[UIColor colorWithWhite:0.200 alpha:1.000]];
        [imageView setImage:[UIImage imageNamed:@"NowPlaying.png"]];
        [nameLabel setFont:[UIFont fontWithName:@"Helvetica Neue UltraLight" size:26.0]];
        [nameLabel setShadowColor:[UIColor colorWithWhite:0.000 alpha:0.650]];
        [nameLabel setShadowOffset:CGSizeMake(1.0, 2.0)];
        [playPauseButton setUserInteractionEnabled:YES];
        [playPauseButton setBackgroundImage:[UIImage imageNamed:@"Pause.png"] forState:UIControlStateNormal];
        [playPauseButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

        [self addSubview:imageView];
        [self addSubview:nameLabel];
        [self addSubview:playPauseButton];
    }
    return self;
}

@end

我剪了一些方法,buttonPressed:(id)sender方法包括在内,不用担心。但UIButton不会响应事件或任何事件。它甚至没有突出自己。
我之前通过一个 nib 文件进行了初始化,[[[NSBundle mainBundle] loadNibNamed:@"BottomBar" [...]] objectAtIndex:0];并连接了IBOutlets(工作正常)和IBActions,这显然不能很好地工作。

任何想法如何解决它?

编辑:我认为它与这部分更相关,因为它似乎UIViewController是重叠的,尽管它被定义为自由形式并且绝对不重叠。我刚刚测试了它,UIWindow颜色设置为红色。

无论如何,这是代码:

OverviewViewController *ovc = [[OverviewViewController alloc] init];
UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:ovc];
[[self window] setRootViewController:nvc];

bottomBar = [[BottomBar alloc] init];
[[self window] addSubview:bottomBar];
[[self window] bringSubviewToFront:bottomBar];

OverviewViewController 在 xib 中定义为 320x460,因此它的底部应该有 44px 用于我的栏。

Edit2:不,调整大小UIViewController不起作用。有什么想法吗?

4

4 回答 4

0

Your code is

playPauseButton = [[UIButton alloc] initWithFrame:CGRectMake(265.0, 2.0, 40.0, 40.0)];

It should be

playPauseButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[playPauseButton setFrame:CGRectMake(265.0, 2.0, 40.0, 40.0)];

The problem is you are not defining the UIButton Type in your code that is needed.

于 2013-11-12T20:26:37.010 回答
0

UIViewController是重叠的UIView,因此无法识别水龙头。

于 2013-11-13T15:16:20.377 回答
0

确保没有任何视图/标签隐藏您的按钮(即使是透明视图也不允许您单击)。还要确保该按钮的边界等于它的框架。

于 2013-11-12T20:03:21.463 回答
0

请注意,大多数时候 imageView 不允许您单击按钮,评论 imageView 代码然后尝试它..

于 2013-11-12T20:10:57.787 回答