自定义后退按钮UI导航栏

时间:2015-12-27 10:14:25

标签: ios objective-c button ios7 uikit

如何为UI导航创建自定义后退按钮

到目前为止,这是我的代码

我是xcode开发的新手

//// General Declarations
CGContextRef context = UIGraphicsGetCurrentContext();

//// Color Declarations
UIColor* color = [UIColor colorWithRed: 0.8 green: 0.32 blue: 0.32 alpha: 1];

//// Rectangle Drawing
UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(18, 104, 263, 93) cornerRadius: 5];
[color setFill];
[rectanglePath fill];


//// Text Drawing
CGRect textRect = CGRectMake(47, 123, 205, 55);
{
    NSString* textContent = @"Back";
    NSMutableParagraphStyle* textStyle = NSMutableParagraphStyle.defaultParagraphStyle.mutableCopy;
    textStyle.alignment = NSTextAlignmentCenter;

    NSDictionary* textFontAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize: 30], NSForegroundColorAttributeName: UIColor.blackColor, NSParagraphStyleAttributeName: textStyle};

    CGFloat textTextHeight = [textContent boundingRectWithSize: CGSizeMake(textRect.size.width, INFINITY)  options: NSStringDrawingUsesLineFragmentOrigin attributes: textFontAttributes context: nil].size.height;
    CGContextSaveGState(context);
    CGContextClipToRect(context, textRect);
    [textContent drawInRect: CGRectMake(CGRectGetMinX(textRect), CGRectGetMinY(textRect) + (CGRectGetHeight(textRect) - textTextHeight) / 2, CGRectGetWidth(textRect), textTextHeight) withAttributes: textFontAttributes];
    CGContextRestoreGState(context);
}

1 个答案:

答案 0 :(得分:0)

只需将leftBarButtonItem更改为自定义按钮即可。

- (void)viewDidLoad {  
  [super viewDidLoad];  

  UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];  
  backBtn.frame = CGRectMake(0, 0, 44, 44);  

  [backBtn setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];  
  [backBtn addTarget:self action:@selector(doBack:) forControlEvents:UIControlEventTouchUpInside];  

  UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];  
  self.navigationItem.leftBarButtonItem = backItem;  

  // Do any additional setup after loading the view.  
}  

-(void)doBack:(id)sender  
{  
  [self.navigationController popViewControllerAnimated:YES];  
}