简体   繁体   中英

iOS: UIPickerView default dimensions

I would like to know the width of the dark grey frame on either side of the inside picker part and the width of the separators between components. This would be at default settings (eg. what's shown in the .xib). Or, how to find such values programmatically. Thank you

To clarify, I'm looking for what the "suggested minimums" might be for those values. I'm putting my UIPickerView in a popover, and the UIPickerView is wider than 320 px (which is what the .xib gives as the "default"). I would like to know these values so I can add them to the popover's width to make sure it doesn't cut off components on the right side of the UIPickerView.

Well this can be a little tricky since the Picker can have a dynamic number of components. So in order to find the widths you have to take into account the number of components. This can be done like so:

        NSInteger n = picker.numberOfComponents;        

        const CGFloat separatorWidth = 8.0f;
        const CGFloat sectionExceedWidth = -2.0f;
        CGFloat totalWidth = picker.bounds.size.width;
        CGFloat panesWidth = totalWidth - separatorWidth * (n - 1);
        for (NSInteger i = 0; i < n; i++) {
            CGFloat sectionWidth = [picker rowSizeForComponent:i].width + sectionExceedWidth;
            panesWidth -= sectionWidth;
        }
        CGFloat leftPaneWidth = ceilf(panesWidth * 0.5f);
        CGFloat rightPaneWidth = panesWidth - leftPaneWidth;
        CGFloat totalHeight = picker.bounds.size.height;
        CGRect totalRect = CGRectMake(0, 0, totalWidth, totalHeight);

Where the left and right panes widths are found and the separator size is static.

This was pulled from here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM