我的一个视图上有一个 MPVolumeView,当有其他输出源可用时,它会出现一个 Airplay 图标。没关系,但是图标很小,无论我为 MPVolumeView 设置多大的框架,它都不会变得更大。
有人知道如何增加播放图标的大小吗?
我的一个视图上有一个 MPVolumeView,当有其他输出源可用时,它会出现一个 Airplay 图标。没关系,但是图标很小,无论我为 MPVolumeView 设置多大的框架,它都不会变得更大。
有人知道如何增加播放图标的大小吗?
我这样做只是为了显示图标并增加其大小:
MPVolumeView *volumeView = [[[MPVolumeView alloc] initWithFrame:CGRectMake(255, 12, 30, 25)] autorelease];
volumeView.showsVolumeSlider = NO;
volumeView.showsRouteButton = YES;
volumeView.transform = CGAffineTransformMakeScale(1.5, 1.5); // increase size by 50%
至少现在,你所能做的就是爬取子视图并手动设置大小。这可能不是一个好主意,因为子视图层次结构可能会发生变化,即使您为图标设置了更大的框架,它也不会变大(或者如果 contentMode 设置为拉伸,您会得到一个模糊的图标)
您甚至可以手动将图标替换为您在应用程序中提供的较大图标,但让我再说一遍,这不是一个好主意。
爬取子视图并使用约束我设法复制了 AVRoutePickerView 的行为,它根据包含的视图调整图标图像的大小。
尽管需要通过 setRouteButtonImage(second image) 使用自定义图标。如果不是,它使用 2 个不显示调整大小的离子的 ImageView(第一张图像)。
接下来附上代码和视图层次结构:
class ViewController: UIViewController {
@IBOutlet weak var airplayView: MPVolumeView!
override func viewDidLoad() {
super.viewDidLoad()
airplayView.showsRouteButton = true
airplayView.showsVolumeSlider = false
airplayView.setRouteButtonImage(UIImage(named: "airplay"), for: .normal)
for view in airplayView.subviews {
if let button = view as? UIButton {
button.imageView?.contentMode = .scaleAspectFit
button.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint(item: button,
attribute: NSLayoutConstraint.Attribute.bottom,
relatedBy: NSLayoutConstraint.Relation.equal,
toItem: airplayView,
attribute: NSLayoutConstraint.Attribute.bottom,
multiplier: 1,
constant: 0).isActive = true
NSLayoutConstraint(item: button,
attribute: NSLayoutConstraint.Attribute.trailing,
relatedBy: NSLayoutConstraint.Relation.equal,
toItem: airplayView,
attribute: NSLayoutConstraint.Attribute.trailing,
multiplier: 1,
constant: 0).isActive = true
NSLayoutConstraint(item: button,
attribute: NSLayoutConstraint.Attribute.top,
relatedBy: NSLayoutConstraint.Relation.equal,
toItem: airplayView,
attribute: NSLayoutConstraint.Attribute.top,
multiplier: 1,
constant: 0).isActive = true
NSLayoutConstraint(item: button,
attribute: NSLayoutConstraint.Attribute.leading,
relatedBy: NSLayoutConstraint.Relation.equal,
toItem: airplayView,
attribute: NSLayoutConstraint.Attribute.leading,
multiplier: 1,
constant: 0).isActive = true
}
}
}
}