【问题标题】:Custom Back Button UI Navigation Bar自定义后退按钮 UI 导航栏
【发布时间】:2016-04-01 11:19:22
【问题描述】:

如何为 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);
}

【问题讨论】:

    标签: ios objective-c button ios7 uikit


    【解决方案1】:

    只需将 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];  
    }  
    

    【讨论】:

      猜你喜欢
      • 2012-01-03
      • 2020-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-30
      • 2019-10-27
      相关资源
      最近更新 更多