我最初动态地在页面上添加了一个MPVolumeView ...
#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;
}
}
但出于某种原因,我开始收到动态添加卷组件时应用程序崩溃的报告。
为了解决这个问题,我决定尝试通过XCode设计器将组件添加到视图中,但后来我意识到我不知道如何做到这一点!
我首先将“对象”从模板拖到“对象”调色板中,但后来我发现无法将其添加到视图中。所以我废弃了这个想法,然后将'View'对象直接拖到.xib视图上。
添加组件后,我尝试将“自定义类”更改为“MPVolumeView”,但视图只渲染了一个空矩形。在模拟器中运行代码无法呈现任何内容。
有没有人知道我缺少哪些步骤将类添加到调色板中不存在的视图中?
不确定你现在是否已经弄明白了。我刚刚创建了一个带有音量滑块的应用程序。这就是我所做的(来自MPVolumeView):
MPVolumeView
#import
@interface MyView : UIViewController {
MPVolumeView *_mpVolumeViewParentView;
}
@property (nonatomic, retain) IBOutlet MPVolumeView *mpVolumeViewParentView;
@end
#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];
}
希望能帮助到你。
添加通用UIView并将自定义类设置为MPVolumeView确实有效。确保执行以下操作:
您应该尝试在iOS设备上运行您的应用程序,而不是模拟器。在模拟器上,MPVolumeView以白色文本显示“无可用卷”。如果视图的背景颜色设置为白色(在Interface Builder中为默认值),则视图显示为空。
更简单:
添加一个视图并将Class更改为MPVolumeView并在.h中声明
#import <MediaPlayer/MediaPlayer.h>
@interface MyView : UIViewController {
MPVolumeView *_mpVolumeview;
}
在.m中只需分配它。
- (void)viewDidLoad {
[super viewDidLoad];
[MPVolumeView alloc];
// set properties using the declared object in .h
// Example
[mpVolumeview setShowsVolumeSlider:NO];
}
找到另一个解决方
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
}
}