maxkonovalov / MKDropdownMenu

🔻 Dropdown Menu for iOS with many customizable parameters to suit any needs
MIT License
527 stars 83 forks source link

How to change title of dropdownmenu #14

Closed vuivenao closed 7 years ago

vuivenao commented 7 years ago

Hi, I want to change the title of DropdownMenu in NavigationBar to the text that selected. Please help me. Thanks!

Dipankardas24 commented 7 years ago

First, declare a NSString property @property (strong, nonatomic) NSString *navTitle;

set your default navigation title at - (void)viewDidLoad like

_navTitle =@"MKDropdownMenu";

change - (NSAttributedString *)dropdownMenu:(MKDropdownMenu *)dropdownMenu attributedTitleForComponent:(NSInteger)component method like this-

- (NSAttributedString *)dropdownMenu:(MKDropdownMenu *)dropdownMenu attributedTitleForComponent:(NSInteger)component {

        return [[NSAttributedString alloc] initWithString:_navTitle
                                               attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:18 weight:UIFontWeightLight],
                                                            NSForegroundColorAttributeName: [UIColor whiteColor]}];
}

and change - (void)dropdownMenu:(MKDropdownMenu *)dropdownMenu didSelectRow:(NSInteger)row inComponent:(NSInteger)component like this-

- (void)dropdownMenu:(MKDropdownMenu *)dropdownMenu didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    NSString *colorString = self.colors[row];
    self.textLabel.text = colorString;
    _navTitle = colorString;

    [self.navigationItem setTitle:colorString];

    UIColor *color = UIColorWithHexString(colorString);
    self.view.backgroundColor = color;
    self.childViewController.shapeView.strokeColor = color;

    delay(0.15, ^{
        [dropdownMenu closeAllComponentsAnimated:YES];
        [dropdownMenu reloadAllComponents];
    });
}