MKDropdownMenu
MKDropdownMenu copied to clipboard
widthForComponent not setting desired width
I am attempting to set a custom width for devices > 375 technically ipads to take a specific amount of width but not matter the width I return the drop down menu's width stays at I guess the default size. In addition, I have set the setUseFullScreenWidth to NO on viewDidLoad, if that makes any difference. This is the code I am using and a screenshot of the simulator on ipadAir2 is attached as well
- (CGFloat)dropdownMenu:(MKDropdownMenu *)dropdownMenu widthForComponent:(NSInteger)component {
if(self.view.frame.size.width > 375) {
[dropdownMenu setUseFullScreenWidth:NO];
return 100.0;
} else {
[dropdownMenu setUseFullScreenWidth:YES];
return 0;
}
}
Hi @serjooo!
If I understood your setup correctly, you have 2 components in your menu.
The width for components is calculated left-to-right. If you provide the same value for all components and the total view width is greater than the sum of all components' widths, then the last (right-most) component will take the rest of the space, ignoring the specified number.
To work around this issue, you can specify the exact width for the desired component and set automatic width for the others:
- (CGFloat)dropdownMenu:(MKDropdownMenu *)dropdownMenu widthForComponent:(NSInteger)component {
if (self.view.frame.size.width > 375) {
[dropdownMenu setUseFullScreenWidth:NO];
return component == 1 ? 100.0 : 0.0;
} else {
[dropdownMenu setUseFullScreenWidth:YES];
return 0;
}
}
Also, you can take a look at the following delegate method:
/// Return NO if the component is used as a dummy space for other components
/// and should not be interacted with. The disclosure indicator is hidden
/// for such components. Default = YES.
- (BOOL)dropdownMenu:(MKDropdownMenu *)dropdownMenu enableComponent:(NSInteger)component;
In case you need some specific layout for your menu, you can add some empty components.
Hope this helps 🙂
Hello,
Sorry I'm late for replying, got really busy and had found a different workaround for this. However, the setup you thought I have is wrong. I have One component and this component I was attempting at giving it different widths according to screen size. However, no matter the size I return, the width of the component was staying at the default size set in the class.
@serjooo I tried to reproduce your issue with one component, but it all worked as expected. The device screen size also didn't matter. Did you have any issues with implementing the same thing with the Demo app? If you provide more details on your menu setup, I'd be happy to help.
I can confirm this bug. I set the width as below, and in iPhone SE it takes over the whole width. The funny thing is in iPhone 6, it the margins work. But in iPhone SE, it is full width no matter what value I set this to.
- (CGFloat)dropdownMenu:(MKDropdownMenu *)dropdownMenu widthForComponent:(NSInteger)component { return CGRectGetWidth([UIScreen mainScreen].bounds)-32.0; } -> dropdown is still the whole width (only in iPhone SE tho)