English 中文(简体)
用箭头在程序上拉回按钮吗?
原标题:Programmatically draw back button with arrow?

通常当您使用 < code> UINAGIGATION 主计长 时,您会得到一个左侧有箭头的后端按钮。 关键是, 我没有使用 UINAVIGATION 主计长, 所以这就是我问的原因。 不管怎么说, 这里有一张图片:

""https://i.sstatic.net/HaYB3.png" alt="此处的内置图像描述"/ >

到目前为止,我有一个常规的 UIBarbuttonTround >,它目前只是平方,左侧没有箭头。

是否有办法可以做一个左侧有箭头的按钮, 并把它添加到我的UINAGATION Bar 上? 我还四处查看过, 似乎UIButton 101 型没有记录, 会导致拒绝。 我需要一个被接受的解决方案!

最佳回答

您可以给按钮一个背景图像, 使其看起来像本地角度按钮 :

testButton = [[UIButton alloc] initWithFrame:CGRectMake(80, 30, 160, 44)];
[testButton setTitle:@"Test Button" forState:UIControlStateNormal];
UIImage *buttonImage = [[UIImage imageNamed:@"angledButton"]  resizableImageWithCapInsets:UIEdgeInsetsMake(0, 16, 0, 16)];
[testButton addTarget:self action:@selector(buttonPressed:) forControlEvents: UIControlEventTouchUpInside];
[testButton setBackgroundImage:buttonImage forState:UIControlStateNormal];

你也许需要调整一下 但我相信方向是正确的

问题回答

Btw. 如果您想要创建与导航控制器中的后按钮完全相同的按钮, 请查看此代码 。

@implementation UIButton (CustomBackButton)

- (UIButton*)configureForBackButtonWithTitle:(NSString*)title target:(id)target action:(SEL)action
{
// Make the text 6 pixels from above, 8 pixels from the right, and 12 pixels from the left of button s frame.
CGFloat padTRL[3] = {6, 8, 12};

// Text must be put in its own UIView, s.t. it can be positioned to mimic system buttons
UILabel* label = [[UILabel alloc] init];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:12];
label.textColor = [UIColor whiteColor];
label.shadowColor = [UIColor darkGrayColor];
label.shadowOffset = CGSizeMake(0, -1);
label.text = title;
[label sizeToFit];

UIImage* norm = [[UIImage imageNamed:@"backBarButton.png"] stretchableImageWithLeftCapWidth:13 topCapHeight:0];
UIImage* click = [[UIImage imageNamed:@"backBarButtonHover.png"] stretchableImageWithLeftCapWidth:13 topCapHeight:0];
[self setBackgroundImage:norm forState:UIControlStateNormal];
[self setBackgroundImage:click forState:UIControlStateHighlighted];
[self addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

// Calculate dimensionss
CGSize labelSize = label.frame.size;
CGFloat controlWidth = labelSize.width+padTRL[1]+padTRL[2];
controlWidth = controlWidth>=norm.size.width?controlWidth:norm.size.width;

// Assemble and size the views
self.frame = CGRectMake(0, 0, controlWidth, 30);
[self addSubview:label];
label.frame = CGRectMake(padTRL[2], padTRL[0], labelSize.width, labelSize.height);
return self
}
@end

不需要使用整幅图像。 只有尖点部分可以使用。 它会根据标题长度拉长图像 。

""https://i.sstatic.net/8mPcu.png" alt="此处输入图像描述"/ >

First of all you have to find an image of the back button. I used a nice app called Extractor that extracts all the graphics from iPhone. In iOS7 I managed to retrieve the image called UINavigationBarBackIndicatorDefault and it was in black colour, since I needed a red tint I change the colour to red using Gimp.

然后,我创造了一个视图, 包含一个带有箭头的图像 View, 一个带有自定义文本的标签, 在视图的顶部有一个带有动作的按钮。 然后我添加了一个简单的动画( 淡化和翻译 ) 。

以下代码模拟后按钮的行为,包括动画。

-(void)viewWillAppear:(BOOL)animated{
        UIImageView *imageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"UINavigationBarBackIndicatorDefault"]];
        [imageView setTintColor:[UIColor redColor]];
        UILabel *label=[[UILabel alloc] init];
        [label setTextColor:[UIColor redColor]];
        [label setText:@"Blog"];
        [label sizeToFit];

        int space=6;
        label.frame=CGRectMake(imageView.frame.origin.x+imageView.frame.size.width+space, label.frame.origin.y, label.frame.size.width, label.frame.size.height);
        UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, label.frame.size.width+imageView.frame.size.width+space, imageView.frame.size.height)];

        view.bounds=CGRectMake(view.bounds.origin.x+8, view.bounds.origin.y-1, view.bounds.size.width, view.bounds.size.height);
        [view addSubview:imageView];
        [view addSubview:label];

        UIButton *button=[[UIButton alloc] initWithFrame:view.frame];
        [button addTarget:self action:@selector(handleBack:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:button];

        [UIView animateWithDuration:0.33 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
            label.alpha = 0.0;
            CGRect orig=label.frame;
            label.frame=CGRectMake(label.frame.origin.x+25, label.frame.origin.y, label.frame.size.width, label.frame.size.height);
            label.alpha = 1.0;
            label.frame=orig;
        } completion:nil];

        UIBarButtonItem *backButton =[[UIBarButtonItem alloc] initWithCustomView:view];
}

- (void) handleBack:(id)sender{
}




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签