自定义背景
语聊房 UIKit (Live Audio Room Kit) 允许您自定义语聊房的背景视图。
实现流程
以实现如下自定义设置为例,将为您举例说明具体实现方式。
- 在左上角显示语聊房的标题和房间 ID。
- 显示自定义背景图片。

具体实现方式如下:
- 创建
AudioRoomBackgroundView
类。
Untitled
class AudioRoomBackgroundView: UIView {
lazy var roomNameLabel: UILabel = {
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 24, weight: .semibold)
label.textAlignment = .left
label.numberOfLines = 1
label.textColor = UIColor.black
return label
}()
lazy var roomIDLabel: UILabel = {
let label = UILabel()
label.textAlignment = .left
label.font = UIFont.systemFont(ofSize: 12, weight: .regular)
label.textColor = UIColor.black
return label
}()
lazy var backgroundImageView: UIImageView = {
let view = UIImageView()
return view
}()
override init(frame: CGRect) {
super.init(frame: frame)
self.addSubview(backgroundImageView)
self.addSubview(roomNameLabel)
self.addSubview(roomIDLabel)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
self.setupLayout()
}
func setupLayout() {
self.backgroundImageView.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height)
self.roomNameLabel.frame = CGRect(x: 27, y: 57, width: self.frame.width - 27, height: 33)
self.roomIDLabel.frame = CGRect(x:27, y: self.roomNameLabel.frame.maxY + 2, width: self.frame.width - 27, height: 21)
}
public func setBackgroundViewContent(_ roomName: String, roomID: String, image: UIImage) {
self.roomNameLabel.text = roomName
self.roomIDLabel.text = roomID
self.backgroundImageView.image = image
}
}
1
- 将
AudioRoomBackgroundView
设置到ZegoUIKitPrebuiltLiveAudioRoomVC
中。
Untitled
class ViewController: UIViewController {
let selfUserID: String = "userID"
let selfUserName: String = "userName"
let yourAppID: UInt32 = YourAppID // 替换为您的 AppID,请从 ZEGO 控制台获取
let yourAppSign: String = "YourAppSign" // 替换为您的 AppSign,请从 ZEGO 控制台获取
let roomID: String = "YourRoomID" // 替换为您的 RoomID
@IBAction func startLiveAudio(_ sender: Any) {
// 在此处修改您的自定义配置。
let config: ZegoUIKitPrebuiltLiveAudioRoomConfig = ZegoUIKitPrebuiltLiveAudioRoomConfig.host()
let liveAudioVC = ZegoUIKitPrebuiltLiveAudioRoomVC.init(yourAppID, appSign: yourAppSign, userID: selfUserID, userName: selfUserName, roomID: roomID, config: config)
liveAudioVC.modalPresentationStyle = .fullScreen
let backgroundView: AudioRoomBackgroundView = AudioRoomBackgroundView(frame: CGRect(x: 0, y: 0, width: liveAudioVC.view.frame.width, height: liveAudioVC.view.frame.height))
backgroundView.setBackgroundViewContent("语聊房", roomID: roomID, image: UIImage(named: "your_background_image_name")) // 替换为实际的图片名称
liveAudioVC.setBackgroundView(backgroundView)
self.present(liveAudioVC, animated: true, completion: nil)
}
}
1