简体   繁体   中英

Custom Back Button UI Navigation Bar

How can I create custom Back Button for UI Navigation

Here is my code so far

I am pretty new to xcode development

//// 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);
}

Just change the leftBarButtonItem to your custom button.

- (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];  
}  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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