UIButtons自定义UIBarButtonItems与自定义图像 – 是否有可能使水龙头目标更大?

我正在做UIBarButtons如下:

// Create "back" UIBarButtonItem UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom]; backButton.frame = CGRectMake(0, 0, 28, 17); [backButton addTarget:self action:@selector(backButtonTapped) forControlEvents:UIControlEventTouchUpInside]; backButton.showsTouchWhenHighlighted = YES; UIImage *backButtonImage = [UIImage imageNamed:@"back-button.png"]; [backButton setBackgroundImage:backButtonImage forState:UIControlStateNormal]; UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton]; [toolBarItems addObject:backBarButtonItem]; 

但是,龙头目标很小。 更确切地说,它们是自定义图像的大小。 (这又是一个小小的问题。)有什么方法可以增加他们的水龙头的大小?

(注:改变UIButtons的框架属性只是拉伸图像。)

对你的代码的小改动就可以做到这一点

需要的变化:

  • 我假设backButtonImage的大小是{28,17}并将button框架设置为CGRectMake(0, 0, 48, 37) `
  • 删除backGroundImage并使用setImage:
  • 将属性imageEdgeInsets设置为UIEdgeInsetsMake(10, 10, 10, 10)

你的代码将变成这样:

 UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom]; backButton.frame = CGRectMake(0, 0, 48, 37); [backButton addTarget:self action:@selector(backButtonTapped) forControlEvents:UIControlEventTouchUpInside]; backButton.showsTouchWhenHighlighted = YES; UIImage *backButtonImage = [UIImage imageNamed:@"back-button.png"]; [backButton setImage:backButtonImage forState:UIControlStateNormal]; backButton.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10); UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton]; [toolBarItems addObject:backBarButtonItem]; 

您可以按照您的要求更改frameimageEdgeInsets的值。
此代码为我工作。

您可以更改UIBarButtonItem的width属性

 backBarButtonItem.width = x; 

不幸的是你不能改变高度的方式,因为没有高度属性。

你可以做什么,但是是通过UIBarButtonItem一个UIButton与定义的框架使用initWithCustomView

例如:

  UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; UIImage *backButtonImage = [UIImage imageNamed:@"back-button.png"]; [button setBackgroundImage:backButtonImage forState:UIControlStateNormal]; button.frame = CGRectMake(0, 0, width, height); UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button]; 

如果图像看起来很紧张,请确保保持相同的高宽比! 或者确保图像尺寸完全相同。

1)添加一个类别到你的UIButton
2)添加新的属性的类别
3)添加你的方法来初始化后退button
4)覆盖-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
5)子类toolBarItems

 @implementation UIButton (yourButtonCategory) @dynamic shouldHitTest; // boolean @dynamic hitTestRect; // enlarge rect of the button @dynamic buttonPressedInterval; // interval of press, sometimes its being called twice -(id)initBackBtnAtPoint:(CGPoint)_point{ // we only need the origin of the button since the size and width are fixed so the image won't be stretched self = [self initWithFrame:CGRectMake(_point.x, _point.y, 28, 17)]; [self setBackgroundImage:[UIImage imageNamed:@"back-button.png"]forState:UIControlStateNormal]; self.shouldHitTest = CGRectMake(self.frame.origin.x - 25, self.frame.origin.y-10, self.frame.size.width+25, self.frame.size.height+25); // this will be the enlarge frame of the button self.shouldHitTest = YES; return self; } -(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{ BOOL shouldReturn = [super pointInside:point withEvent:event]; NSSet *touches = [event allTouches]; BOOL shouldSendTouches = NO; for (UITouch *touch in touches) { switch (touch.phase) { case UITouchPhaseBegan: shouldSendTouches = YES; break; default: shouldSendTouches = NO; break; } } if(self.shouldHitTest){ double elapse = CFAbsoluteTimeGetCurrent(); CGFloat totalElapse = elapse - self.buttonPressedInterval; if (totalElapse < .32) { return NO; // not the event we were interested in } else { // use this call if(CGRectContainsPoint(self.hitTestRect, point)){ if(shouldSendTouches){ self.buttonPressedInterval = CFAbsoluteTimeGetCurrent(); [self sendActionsForControlEvents:UIControlEventTouchUpInside]; } return NO; } } } return shouldReturn; } -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [super touchesBegan:touches withEvent:event]; UITouch *touch = [touches anyObject]; CGPoint touch_point = [touch locationInView:self]; [self pointInside:touch_point withEvent:event]; } @end 

可以说触摸事件不会触发我们需要它的视图来调用button,所以在toolBarItems我们做的事情就像:

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [super touchesBegan:touches withEvent:event]; for(id subs in self.subviews){ if([subs isKindOfClass:[UIButton class]]){ [subs touchesBegan:touches withEvent:event]; } } } 

那就是这样。 我们放大框架而不放大实际的button。

你只需要像下面这样初始化button: UIButton *btn = [[UIButton alloc]initBackBtnAtPoint:CGPointMake(0,0)];

希望能帮助到你

嗯…你需要达到的目标 – 那就是没有形象的延伸 – 有一个简单的方法:

1)使用GIMP或Photoshop,并在您的图像下面创build一个透明图层,以便它匹配您想要的大小。

2)合并下来,创build视网膜和非视网膜图像。

3)更新您的代码,以便它反映新的图像大小。

4)分配图像,然后运行你的代码。

这样你的原始图像将不会被拉伸,因为它的边界将考虑到它的透明部分。

除此之外,你可以用编程的方式来完成这个任务,但是我怀疑这是一个好主意,除非你计划为了其他原因而进入UIGraphics。

当你调用initWithCustomViewUIBarButtonItem将事件处理委托给自定义视图,在你的情况下,它是UIButton 。 反过来, UIButton正在从图像中获取边界,并且作为背景图像,预计会根据需要进行扩展以填充新的边界。

imageInsets直接在UIBarButtonItem上设置imageimageInsets属性。 这些属性在父类UIBarItem 。 你也可能希望设置他们的景观变种。

将button框更改大,并更改您的线

 [backButton setBackgroundImage:backButtonImage forState:UIControlStateNormal]; 

至:

 [backButton setImage:backButtonImage forState:UIControlStateNormal]; 

只是使用

 [button setImage:backButtonImage forState:UIControlStateNormal]; 

代替

 [backButton setBackgroundImage:backButtonImage forState:UIControlStateNormal]; 

并将框架设置为任何你喜欢的。 图像将被居中,button将在给定的框架中接收触摸。

像这样的东西:

 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(50, 50, 50, 50); [button setImage:backButtonImage forState:UIControlStateNormal]; 

你应该能够在button上设置边缘插图。

所以设置button的边框大于图像,然后在button上设置边缘插图。

所以要扩大你的宽度每边10像素应该这样的工作

 backButton.frame = CGRectMake(0,0,28,37); backButton.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 10); 

增加UIButton框架(直到您想要的水龙头大小)

  button.frame = CGRectMake(0, 0, 56, 20); 

如果你想看完整的button图像,请写下面的方法

  [backButton setBackgroundImage:backButtonImage forState:UIControlStateNormal]; 

如果你想看到图像的实际大小(不要担心,水龙头的大小只是你的button大小)

  [backButton setImage:backButtonImage forState:UIControlStateNormal]; 

最后

  UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton]; 

希望它可以帮助你!

你需要改变三行代码,并希望它的工作。

1)根据需要更改backButton宽度。

2)设置backButton的Image而不是backButton的BackgroundImage

 [backButton setImage:backButtonImage forState:UIControlStateNormal]; 

3)还设置backButton的contentHorizontalAlignment

 backButton.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;