maxkonovalov / MKDropdownMenu

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

Issue with dropdownShowsBorder and setSeperatorColor #16

Closed EddieLukeAtmey closed 7 years ago

EddieLukeAtmey commented 7 years ago

Hi, the border (in #6) works pretty fine, except that something with dropdownShowsBorder == YES in line 752~756:

if (self.showsBorder && indexPath.row == self.rowsCount - 1) {
        cell.separatorInset = UIEdgeInsetsMake(0, CGRectGetWidth(tableView.bounds), 0, 0);
} else {
        cell.separatorInset = UIEdgeInsetsZero;
}

I'm not sure why you have to set seperatorInset to the last row when showing border, but it seems to crop the data inside the cell and my label turn from any to "..." string. I've tried comment out everything except cell.separatorInset = UIEdgeInsetsZero; and it turns out fine.

Also, there seems to have a problem with rowSeparatorColor, as it affect the border color but doesn't to the seperator (image below). image

Though I agree that the border color should be the same as the separator's, please do something with these, thanks!!! (I was checking in ios 10.2)

maxkonovalov commented 7 years ago

Hello @EddieLukeAtmey, I'll check the issue and will let you know of the fix.

maxkonovalov commented 7 years ago

@EddieLukeAtmey I tried to reproduce your issue but all separator colors in the Example project are displayed correctly, as well as no issues with truncated label. Could you provide more details on your setup?

EddieLukeAtmey commented 7 years ago

@maxkonovalov I subclass your control and customize it to fit my stuff:

@interface MyDropDownMenu : MKDropdownMenu

/*! Attributed String Dictionary for Normal text. */
@property (strong, nonatomic) NSDictionary *attributeString;

@end

And then I have some stuff when initialze control (call in initWith...: and awakeFromNib). Currently I don't use separatorColor yet. That day I just found out the property and try it out to see how it work.

- (void)setupDropDown
{
    // Setup dropdown
    self.dropdownDropsShadow = NO;
    self.dataSource = self;
    self.delegate = self;
    self.backgroundDimmingOpacity = 0;
    self.dropdownBouncesScroll = NO;

    self.disclosureIndicatorImage = [UIImage imageNamed:@"ic_expand"];
    // setup later.
    self.selectedDatas = @[];
    self.selectedIndexes = @[];
}

And then stuff for selected row

- (NSDictionary *)selectedAttributeString {  return _selectedAttributeString ?: self.attributeString; }
- (void)setAttributeString:(NSDictionary *)attributeString
{
    _attributeString = attributeString;

    // Make selected font bolder
    if (attributeString[NSFontAttributeName]) {
        UIFont *font = attributeString[NSFontAttributeName];
        UIFont *selectedFont = [UIFont fontWithDescriptor:[[font fontDescriptor] fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold]
                                                     size:font.pointSize + 2];

        if (selectedFont) {
            NSMutableDictionary *selectedAttribute = [attributeString mutableCopy];
            [selectedAttribute setObject:selectedFont forKey:NSFontAttributeName];
            self.selectedAttributeString = selectedAttribute;
        }
    }
}

- (NSAttributedString *)dropdownMenu:(MKDropdownMenu *)dropdownMenu attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSString *data = self.dataList[row];
    NSDictionary *attribute = [self.selectedDatas containsObject:data] ? self.selectedAttributeString : self.attributeString;

    NSAttributedString *value = [[NSAttributedString alloc] initWithString:data
                                                                attributes:attribute];

    return value;
}

Other stuffs

- (NSInteger)dropdownMenu:(MKDropdownMenu *)dropdownMenu numberOfRowsInComponent:(NSInteger)component { return self.dataList.count; }
- ( CGFloat )dropdownMenu:(MKDropdownMenu *)dropdownMenu rowHeightForComponent:(NSInteger)component { return self.frame.size.height; }
- (UIColor *)dropdownMenu:(MKDropdownMenu *)dropdownMenu
    backgroundColorForRow:(NSInteger)row forComponent:(NSInteger)component { return self.backgroundColor; }

Update: I've tried comment out all those attributedString, event replace the protocol impl to the basic one titleForRow:inComponent:. Still the same. Probably not because of attributedString

maxkonovalov commented 7 years ago

I tried to replicate your setup - still no issues. Are you sure you don't set layer.borderColor or something similar manually in your code? You can try commenting out more parts of your subclass code until the issue goes away to identify the problem.

EddieLukeAtmey commented 7 years ago

Okay, so I've commented out almost every customized stuff. Everything went back to normal. But once I set self.dropdownShowsBorder = NO the label is truncated (separator still working fine). And that's really the cause for the issue (refer to the origin question).

simulator screen shot mar 2 2017 15 53 27

Check images below, the width of the label is 15, which is why it's truncated. Reason related to separator inset. screen shot 2017-03-02 at 15 58 27 screen shot 2017-03-02 at 15 58 16

I'll check again how to reproduce the issue with separator border.

maxkonovalov commented 7 years ago

Great! We're getting closer :) It's really strange that table view cell separator affects the content...

maxkonovalov commented 7 years ago

I think it might be related to the layout margins somehow.

Can you try adding this line:

cell.preservesSuperviewLayoutMargins = NO;

to the -tableView:cellForRowAtIndexPath: in MKDropdownMenu.m line 754, just after cell.layoutMargins = UIEdgeInsetsZero;

Also can try to add:

self.tableView.layoutMargins = UIEdgeInsetsZero;

@EddieLukeAtmey let me know if the above changes anything. If not, we should keep commenting out the code to find the reason of the issue :)

EddieLukeAtmey commented 7 years ago

Alright, I've try adding cell.preservesSuperviewLayoutMargins = NO; in the line you suggest, not working.

About self.tableView.layoutMargins = UIEdgeInsetsZero, you've already set it in viewDidLoad (Line 419)

maxkonovalov commented 7 years ago

I see... So the only thing is to keep searching for the cause of this now

EddieLukeAtmey commented 7 years ago

As you commented the rejection of the #18 pull request, about the last row separator, how about we try using this solution:

self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 1)];

maxkonovalov commented 7 years ago

Ok, I updated the bottom row separator logic to use footer view - see bb021f60825c40507338deb34670d63a72ac20bd

EddieLukeAtmey commented 7 years ago

For my issue, your fix works like a charm

simulator screen shot mar 2 2017 17 37 51

But it seems that solution didn't solve the last line separator screen shot 2017-03-02 at 17 37 41

I think it should be like this

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{        
    return [[UIView alloc] initWithFrame:CGRectZero];
}

Also, I have to specify your commit to pod file instead of general pod: pod 'MKDropdownMenu', :git => 'https://github.com/maxkonovalov/MKDropdownMenu.git', :commit => 'bb021f6'

maxkonovalov commented 7 years ago

Can you please check the example project - the fix works fine there. Also, you need to set dropdownShowsBottomRowSeparator = NO because it's enabled by default.

EddieLukeAtmey commented 7 years ago

Work like a charm! Thank you very much!!!

maxkonovalov commented 7 years ago

@EddieLukeAtmey you're welcome :) The changes are now available in cocoapods 🎉