How to add MPVolumeView via Xcode Designer?

I initially added MPVolumeView dynamically to the page ...

#import "MediaPlayer/MPVolumeView.h"
.
.
-(IBAction) handleVolumeButtonClicked:(id) sender {
    if (volumeView == nil) {
        volumeView = [[MPVolumeView alloc] initWithFrame:CGRectMake(25, 378, 270, 30)];
        [self.view addSubview:volumeView];
        [volumeView release];
    } else {
        [volumeView removeFromSuperview];
        volumeView = nil;
    }
}

But for some reason, I started getting crash reports when dynamically adding a volume component.

To get around this, I decided to try adding the component to the view through the Xcode constructor, but then I realized that I did not know how to do it!

I first dragged the "Object" from the template into the "Object" panel, but then I found that I could not add it to the view. So I gave up on this idea and then dragged the "View" object directly onto the .xib view.

" " "MPVolumeView", . .

- , , , ?

+4
6

, . . ( MPVolumeView):

  • UIView, .xib.
  • MPVolumeView
  • .h :
#import 

@interface MyView : UIViewController {
    MPVolumeView *_mpVolumeViewParentView;
}
@property (nonatomic, retain) IBOutlet MPVolumeView *mpVolumeViewParentView;

@end
  • .m :
#import "MyView.h"

@implementation MyView

@synthesize mpVolumeViewParentView = _mpVolumeViewParentView;

- (void)viewDidLoad {
    [super viewDidLoad];
    [[self mpVolumeViewParentView] setBackgroundColor:[UIColor clearColor]];
    MPVolumeView *myVolumeView = [[MPVolumeView alloc] initWithFrame:[[self mpVolumeViewParentView] bounds]];
    [[self mpVolumeViewParentView] addSubview:myVolumeView];
    [myVolumeView release];
}
- (void)dealloc {
    [_mpVolumeViewParentView release];
    [super dealloc];
}

, .

-1

UIView MPVolumeView . :

  • MediaPlayer . MPVolumeView ( , ), .
  • . .
  • , .
+23

iOS, . MPVolumeView " " . ( Interface Builder), .

+5

:

MPVolumeView .h

    #import <MediaPlayer/MediaPlayer.h>

@interface MyView : UIViewController {
    MPVolumeView *_mpVolumeview;
}

In .m just highlight it.

- (void)viewDidLoad {
    [super viewDidLoad];
    [MPVolumeView alloc];

    // set properties using the declared object in .h
    // Example
    [mpVolumeview setShowsVolumeSlider:NO]; 
}
+4
source

I slightly changed the decision of Roman Aliyev .

You can set MPVolumeView as a subclass of UISlider

@IBOutlet weak var volume: MPVolumeView!

let volumeSetting = volume.subviews.first as? UISlider
    volumeSetting?.minimumValueImage = UIImage(named: "SoundQuiet")
    volumeSetting?.maximumValueImage = UIImage(named: "SoundLoud")
    ...
+1
source

Found another solution.

  • Add UISlider via Xcode Interface Builder.
  • Customize its look in IB.
  • Programmatically replace it with MPVolumeView as follows:
class PlayerView: UIView {
    @IBOutlet weak var volumeViewMock: UISlider!

    override func awakeFromNib() {
        super.awakeFromNib()
        self.volumeViewMock.replaceWithMPVolumeView()
    }
}

extension UISlider {
    func replaceWithMPVolumeView() {
        // do this only once
        guard !self.isHidden else { return }

        let volumeView = MPVolumeView()
        volumeView.showsRouteButton = false

        // get the slider of volumeView and configure it in the same way as the IB slider
        guard let volumeViewSlider = volumeView.subviews.first as? UISlider else { return }
        volumeViewSlider.minimumValueImage = self.minimumValueImage
        volumeViewSlider.maximumValueImage = self.maximumValueImage
        volumeViewSlider.tintColor = self.tintColor
        volumeViewSlider.minimumTrackTintColor = self.minimumTrackTintColor
        volumeViewSlider.maximumTrackTintColor = self.maximumTrackTintColor
        volumeViewSlider.thumbTintColor = self.thumbTintColor

        guard let parentView = self.superview else { return }
        parentView.addSubview(volumeView)

        // frame of volumeView and frame of the IB slider should be the same
        volumeView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            volumeView.topAnchor.constraint(equalTo: self.topAnchor),
            volumeView.bottomAnchor.constraint(equalTo: self.bottomAnchor),
            volumeView.leadingAnchor.constraint(equalTo: self.leadingAnchor),
            volumeView.trailingAnchor.constraint(equalTo: self.trailingAnchor),
            ])

        // hide the IB slider
        self.isHidden = true
    }
}
0
source

All Articles