在UIToolBar中匹配MPVolumeView和UISlider垂直位置

Kaj*_*ala 6 objective-c uitoolbar ios mpvolumeview

我有一个UIToolBar,旨在包含用于控制音量和亮度的滑块.我为音量使用MPVolumeView滑块,为亮度使用普通的UISlider.虽然滑块本身工作正常,但它们的垂直位置不匹配:

不匹配的UISlider和MPVolumeView

如何让它们处于同一高度?

我的代码:

- (void) createToolbar{

toolBar = [[UIToolbar alloc] init];
toolBar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);

UISegmentedControl *modeSelector = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Play", @"Rec", nil]];
[modeSelector setSegmentedControlStyle:UISegmentedControlStyleBar];
[modeSelector addTarget:self action:@selector(changePlayMode) forControlEvents:UIControlEventValueChanged];
modeSelector.selectedSegmentIndex = 0;
UIBarButtonItem *modeSelectorAsToolbarItem = [[UIBarButtonItem alloc] initWithCustomView:modeSelector];

brightnessSlider = [[UISlider alloc] initWithFrame:CGRectMake(0, 0, 150, 30)];
brightnessSlider.minimumValue = 0;
brightnessSlider.maximumValue = 1;
brightnessSlider.value = [[UIScreen mainScreen] brightness];
brightnessSlider.continuous = YES;
[brightnessSlider addTarget:self action:@selector(adjustBrightness:) forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *brightSliderAsToolbarItem = [[UIBarButtonItem alloc] initWithCustomView:brightnessSlider];

MPVolumeView *volView = [[MPVolumeView alloc] initWithFrame:CGRectMake(0, 0, 150, 30)];
volView.showsRouteButton = NO;
UIBarButtonItem *volSliderAsToolbarItem = [[UIBarButtonItem alloc] initWithCustomView:volView];

UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *toc = [[UIBarButtonItem alloc] initWithTitle:@"Contents" style:UIBarButtonItemStyleBordered target:self action:@selector(goToToC)];
[toolBar setItems:[NSArray arrayWithObjects:modeSelectorAsToolbarItem, flexibleSpace, brightSliderAsToolbarItem, volSliderAsToolbarItem, flexibleSpace, toc, nil] animated:NO];

toolBar.autoresizingMask |= UIViewAutoresizingFlexibleWidth;

[[self view] addSubview:toolBar];

}
Run Code Online (Sandbox Code Playgroud)

(更改CGRectMake坐标似乎没有做任何事情.)

问题" 自定义MPVolumeView拇指图像自iOS 5.1以来没有垂直居中 "的评论似乎建议使用这里解释的"修复拇指对齐"技巧,但实现该代码似乎没有做任何事情,据我所知,和我不确定是不是在谈论同样的问题.

kmi*_*ael 20

如果创建了一个简单的子类MPVolumeView和覆盖volumeSliderRectForBounds:,则可以为滑块rect定义自己的对齐方式.我想返回整个边界,它将滑块置于MPVolumeView框架中心

@interface KMVolumeView : MPVolumeView

@end

@implementation KMVolumeView

- (CGRect)volumeSliderRectForBounds:(CGRect)bounds
{
    return bounds;
}

@end
Run Code Online (Sandbox Code Playgroud)

只需在代码或界面构建器中使用您的子类,然后就可以可靠地定位卷视图.

  • 鉴于MPVolumeView是在iOS 2.0中引入的,而那些自定义API是在iOS 6.0中引入的,@ EricArmstrong可能只是过时的注释。 (2认同)

hyp*_*asm 15

我想出了一些东西,这扩展了kmikael的答案:

@interface SystemVolumeView : MPVolumeView

@end

@implementation SystemVolumeView

- (CGRect)volumeSliderRectForBounds:(CGRect)bounds {
    CGRect newBounds=[super volumeSliderRectForBounds:bounds];

    newBounds.origin.y=bounds.origin.y;
    newBounds.size.height=bounds.size.height;

    return newBounds;
}

- (CGRect) routeButtonRectForBounds:(CGRect)bounds {
    CGRect newBounds=[super routeButtonRectForBounds:bounds];

    newBounds.origin.y=bounds.origin.y;
    newBounds.size.height=bounds.size.height;

    return newBounds;
}

@end
Run Code Online (Sandbox Code Playgroud)

此实现的不同之处在于它仍使用默认水平值但覆盖垂直值,以使MPVolumeView在其容器中保持垂直居中.它也会覆盖,-routeButtonRectForBounds:以便播放/路线按钮也会居中.

我不得不想知道为什么默认实现具有不稳定的垂直位置.


Jer*_*ome 5

SWIFT 版本: 来自过度痉挛的答案:

import Foundation

class SystemVolumeView: MPVolumeView {
    override func volumeSliderRect(forBounds bounds: CGRect) -> CGRect {
        var newBounds = super.volumeSliderRect(forBounds: bounds)
        newBounds.origin.y = bounds.origin.y
        newBounds.size.height = bounds.size.height
        return newBounds
    }
    override func routeButtonRect(forBounds bounds: CGRect) -> CGRect {
        var newBounds = super.routeButtonRect(forBounds: bounds)
        newBounds.origin.y = bounds.origin.y
        newBounds.size.height = bounds.size.height
        return newBounds
    }
}
Run Code Online (Sandbox Code Playgroud)