2011-09-05 25 views
9

Làm cách nào để xóa hiệu ứng bóng/bóng khỏi các nút trên thanh điều hướng? Nếu tôi tùy chỉnh thanh điều hướng bằng cách sử dụng hình ảnh tùy chỉnh, các nút không bị ảnh hưởng, tôi có thể xóa hiệu ứng khỏi chúng (dòng và độ bóng) hoặc xác định mã màu hex cho toàn bộ nút hoặc thậm chí là hình ảnh tùy chỉnh cho chúng quá?Loại bỏ hiệu ứng tỏa sáng khỏi các nút trong UINavigationBar

+1

@Rudy - Nếu bạn cho rằng câu hỏi là trùng lặp với một câu hỏi khác, vui lòng bỏ phiếu để đóng câu hỏi thay vì chỉnh sửa nội dung của câu hỏi. –

+0

Đây là bản sao có thể có của [iOS: Cách thay thế nền của thanh tìm kiếm tỏa sáng/phát sáng bằng một màu] (http://stackoverflow.com/questions/6696200/ios-how-to-replace-search-bar-background- tỏa sáng rực rỡ với một màu) –

Trả lời

18

Tôi vừa trải qua quá trình tìm hiểu điều này. Về cơ bản, bạn cần tạo các hình ảnh có thể kéo giãn tùy chỉnh và sử dụng chúng làm nền của nút để loại bỏ độ sáng. Thay thế các nút quay lại trong một UINavigationController là một chút khó khăn hơn. Cho rằng tôi đã sử dụng một UINavigationControllerDelegate để thay thế nút quay lại mặc định bằng nút tùy chỉnh của tôi.

Dưới đây là một số mã:

  1. Tạo một thể loại trên UIBarButtonItem tạo nút tùy chỉnh của bạn. Đây là của tôi. Tôi sử dụng loại này để tùy chỉnh cả hai nút thanh thường xuyên và trở lại nút:

    @interface UIBarButtonItem (UIBarButtonItem_customBackground) 
    
    + (id) customBarButtonWithTitle:(NSString *)title target:(id)target selector:(SEL)selector; 
    + (id) customBackButtonWithTitle:(NSString *)title target:(id)target selector:(SEL)selector; 
    
    @end 
    
    @implementation UIBarButtonItem (UIBarButtonItem_customBackground) 
    
    + (id) customButtonWithImageNamed:(NSString *)imageName selectedImageNamed:(NSString *)selectedImageName leftCapWidth:(CGFloat)leftCapWidth edgeInsets:(UIEdgeInsets)edgeInsets title:(NSString *)title target:(id)target selector:(SEL)selector { 
        UIButton* customButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
        [customButton addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside]; 
        customButton.titleLabel.font = [UIFont boldSystemFontOfSize:12.0f]; 
        customButton.titleLabel.shadowColor = [UIColor colorWithRed:0.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:0.25f]; 
        customButton.titleLabel.shadowOffset = CGSizeMake(0.0f, -1.0f); 
        customButton.titleLabel.lineBreakMode = UILineBreakModeTailTruncation; 
        customButton.titleEdgeInsets = edgeInsets; 
        UIImage* navButtonBackgroundImage = [[UIImage imageNamed:imageName] stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:0.0f]; 
        UIImage* navButtonPressedBackgroundImage = [[UIImage imageNamed:selectedImageName] stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:0.0f]; 
        [customButton setBackgroundImage:navButtonBackgroundImage forState:UIControlStateNormal]; 
        [customButton setTitle:title forState:UIControlStateNormal]; 
        [customButton setBackgroundImage:navButtonPressedBackgroundImage forState:UIControlStateHighlighted]; 
        [customButton setBackgroundImage:navButtonPressedBackgroundImage forState:UIControlStateSelected]; 
    
        CGSize size = CGSizeMake(30.0f, 30.0f); 
        if (title != nil) { 
         size = [[NSString stringWithString:title] sizeWithFont:customButton.titleLabel.font]; 
        } 
        customButton.frame = CGRectMake(0.0f, 0.0f, size.width + 20.0f, 30.0f); 
        customButton.layer.shouldRasterize = YES; 
        customButton.layer.rasterizationScale = [[UIScreen mainScreen] scale]; 
        return [[[UIBarButtonItem alloc] initWithCustomView:customButton] autorelease]; 
    } 
    
    + (id) customBarButtonWithTitle:(NSString *)title target:(id)target selector:(SEL)selector { 
        return [self customButtonWithImageNamed:@"navButtonBG.png" 
           selectedImageNamed:@"navButtonPressedBG.png" 
             leftCapWidth:6.0f 
             edgeInsets:UIEdgeInsetsMake(0.0f, 5.0f, 0.0f, 5.0f) 
               title:title 
              target:target 
              selector:selector]; 
    } 
    
    + (id) customBackButtonWithTitle:(NSString *)title target:(id)target selector:(SEL)selector {  
        return [self customButtonWithImageNamed:@"backButtonBG.png" 
           selectedImageNamed:@"backButtonPressedBG.png" 
             leftCapWidth:12.0f 
             edgeInsets:UIEdgeInsetsMake(0.0f, 11.0f, 0.0f, 5.0f) 
               title:title 
              target:target 
              selector:selector]; 
    } 
    
    @end 
    
  2. Thêm nút để UINavigationBar

    UIBarButtonItem* logoutButton = [UIBarButtonItem customBarButtonWithTitle:@"Logout" target:self selector:@selector(logout)]; 
    self.navigationItem.rightBarButtonItem = logoutButton; 
    
  3. của bạn Nếu bạn cũng muốn thay thế nút lưng UINavigationController, thiết lập một UINavigationControllerDelegate và triển khai phương thức willShowViewController như sau:

    - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated { 
        if([navigationController.viewControllers count ] > 1) { 
         UIViewController* backViewController = [navigationController.viewControllers objectAtIndex:(navigationController.viewControllers.count - 2)]; 
         NSString* backText = backViewController.title; 
         UIBarButtonItem* newBackButton = [UIBarButtonItem customBackButtonWithTitle:backText target:navigationController selector:@selector(popViewControllerAnimated:)]; 
         viewController.navigationItem.leftBarButtonItem = newBackButton; 
         viewController.navigationItem.hidesBackButton = YES; 
        } 
    } 
    
  4. Đây là stretc hình ảnh hable Tôi đang sử dụng:

    • Trở lại nút: back button Pressed: enter image description here
    • nút thường xuyên: enter image description here Pressed: enter image description here
+0

BẠN. LÀ VÌ THẾ. FEAKING. TUYỆT VỜI!!!!! Anh yêu em! – Eugene

+0

@Justin Gallagher Justin cảm ơn bạn cảm ơn bạn cảm ơn bạn! –

+0

Làm cách nào để thêm mã này vào một dự án để sử dụng? –

0

bạn phải sử dụng nút tùy chỉnh có hình ảnh mà không có bất kỳ hiệu ứng bóng nào trên hình ảnh mà bạn có thể loại bỏ hiệu ứng gloos của nút từ thanh điều hướng.

2

Đối với việc thay đổi nút quay lại nó không phải là cần thiết để thực hiện các uinavigationcontroller phương pháp đại biểu.

Bạn chỉ cần đặt thuộc tính hidesBAckButton thành YES sau khi đặt nút back mong muốn như @Justin Gallacher giải thích một cách hoàn hảo.

self.navigationItem.leftBarButtonItem = [UIBarButtonItem customBackButtonWithTitle:@"Back" target:self.navigationController selector:@selector(popViewControllerAnimated:)]; 
self.navigationItem.hidesBackButton = YES; 
+0

Điều này thậm chí không cần thiết, thiết lập thuộc tính leftBarButtonItem sẽ ghi đè nút quay lại – Daniel

Các vấn đề liên quan