简体   繁体   English

将自定义UIButton添加到UINavigationBar

[英]adding custom UIButton to UINavigationBar

So the code I have is as follows: 所以我的代码如下:

// Create a containing view to position the button
    UIView *containingView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 23.5, 21.5)] autorelease];

    // Create a custom button with the image
    UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"Settings.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(settings) forControlEvents:UIControlEventTouchUpInside];
    [button setFrame:CGRectMake(-19, -3, 21.5, 21.5)];
    [containingView addSubview:button];

    // Create a container bar button
    UIBarButtonItem *containingBarButton = [[[UIBarButtonItem alloc] initWithCustomView:containingView] autorelease];
    self.navigationItem.rightBarButtonItem = containingBarButton;

The issue is that my Settings.png original image size is 21.5, 21.5 and there fore the button is super hard to click. 问题是我的Settings.png原始图像大小为21.5、21.5,因此该按钮很难单击。 I have to tap really hard in order to trigger the UITouchUpInside to be triggered. 我必须非常努力地点击才能触发UITouchUpInside。 This will clearly be rejected if I put it in the app store, as it is not in compliance with apple's HIG. 如果我将其放在应用程序商店中,这显然会被拒绝,因为它不符合Apple的HIG。 Is there a way around this? 有没有解决的办法? I still need to use UIView as the container for the UIButton as I'd like to position it correctly on the UINavigationBar 我仍然需要将UIView用作UIButton的容器,因为我想将其正确放置在UINavigationBar上

Try to set a bigger fram to the button like 40,40 and set the content mode to be the center so the image will apear to be in the center. 尝试为按钮设置一个更大的帧,例如40,40,然后将内容模式设置为中心,这样图像就会显示在中心。

button.contentMode = UIViewContentModeCenter;

Tell me if that helped 告诉我是否有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM