CPTabView insertTabViewItem:atIndex: is not working correctly when items are added between other tabs. The resulting tab's sizes are not updated:
The probable cause
The problem, I think its caused by the way tabs are inserted:
After adding the CPTabViewItem to the _items array the CPTabView calls _updateItems:
This method increments the segment count (this adds a new element at the end) and then iterates over the segments renaming them.
The problem is that renaming only triggers a resizing of the segment when its width is zero. (See here) The issue with this, is that if the element you are inserting is in the middle it will not get its size recalculated, since this is not a new element and it already has a width (The new element was added at the end).
This might be related to issue #301
I can think of several solutions, one of them might be to make CPSegmentedControl be able to insert the new element where you need it. Another one might be to have CPSegmentedControl recalculate the size of every element that gets its label changed.
Milestone: Someday. Label: #new. What's next? A reviewer should examine this issue.
Milestone: Someday. Labels: AppKit, bug. What's next? A reviewer should examine this issue.
Test: expose a problem with - CPTabView insertTabViewItem:atIndex.
Refs #1920, #1921.
CPSegmentedControl : -insertSegmentsAtIndexes: -removeSegmentsAtIndex…
…es: . WIP. Trying to fix #1920. Test in Manual/CPSegmentedControl/
I've been trying to fix this by implementing -insertSegment:atIndex: in CPSegmentedControl. It's trivial to allow insertion/deletion in the collection of individual segments, but there is still a relayout bug -> see CPSegmentedControl manual test - button "remove selected segment".
The layout code in CPSegmentedControl is awfully complicated, I guess because it uses _CPImageAndTextView . Does someone familiar with this code have ideas about how to make it more simple ? Maybe by putting more layout stuff in the segment object.
Hi @cacaodev, I quickly took a look to this problem.
From what you have done, I saw two issues :
If you do
var count = [_segments count];
if (_selectedSegment >= count)
_selectedSegment = -1;
var thickness = [self currentValueForThemeAttribute:@"divider-thickness"],
height = CGRectGetHeight([self bounds]),
dividerExtraSpace = (count - 1) * thickness,
widthOfAllSegments = 0;
for (var i = 0; i < count; i++)
widthOfAllSegments += [_segments[i] width];
[self setFrameSize:CGSizeMake(widthOfAllSegments + dividerExtraSpace, height)];
and comment the lines 726 to 732 it should work.
But this is not an optimize solution at all
Ok, thanks Alex. I thought this methods meant "tile starting at segment". now it works.
i do not see this issue anymore. @cacaodev can we close?
this is fixed.
Milestone: Someday. Labels: #works-for-me, AppKit, bug. What's next? Attempts to reproduce the problem described by this issue have failed to reveal any erroneous situation.