6

ボリュームと明るさを制御するためのスライダーを含めることを目的とした 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];

}

(CGRectMake 座標を変更しても何も起こらないようです。)

「 iOS 5.1 以降、カスタム MPVolumeView Thumb Image が垂直方向に中央揃えされていない」という質問のコメントは、ここで説明されている「親指の配置を修正する」トリックの使用を示唆しているように見えましたが、そのコードを実装しても、私が知る限り、何もしないようでした。それが同じ問題について話していたかどうかはわかりません。

4

4 に答える 4

20

MPVolumeViewと overrideの自明なサブクラスを作成するvolumeSliderRectForBounds:と、スライダー rect の独自の配置を定義できます。境界全体を返すのが好きです。これにより、スライダーがMPVolumeViewのフレームの中央に配置されます

@interface KMVolumeView : MPVolumeView

@end

@implementation KMVolumeView

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

@end

サブクラスをコードまたはインターフェイス ビルダーで使用するだけで、ボリューム ビューを確実に配置できます。

于 2013-11-19T11:01:08.077 に答える
6

SWIFTバージョン: hyperspasmの答えから来る:

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
    }
}
于 2016-11-16T02:18:23.470 に答える
0

MPVolumeViewを使用する代わりに、「システムボリュームの取得」「システムボリュームの変更」の回答に記載されている手順を使用して、両方にUISliderを使用してボリュームスライダーを作成しました。

于 2013-06-01T17:34:04.430 に答える