IOS[Swift版]常用操作代码片段

设置状态栏背景颜色
func setStatusBarBackgroundColor(color : UIColor) {
        let statusBarWindow : UIView = UIApplication.shared.value(forKey: "statusBarWindow") as! UIView
        let statusBar : UIView = statusBarWindow.value(forKey: "statusBar") as! UIView
        /*
         if statusBar.responds(to:Selector("setBackgroundColor:")) {
         statusBar.backgroundColor = color
         }*/
        if statusBar.responds(to:#selector(setter: UIView.backgroundColor)) {
            statusBar.backgroundColor = color
        }
    }

设置效果


设置状态栏字体颜色

状态字体颜色设置分为两种情况,当有页面有NavigationController,时需要这样进行设置:

self.navigationController?.navigationBar.barStyle = .blackTranslucent;

当没有NavigationController控件时,在ViewController中重写preferredStatusBarStyle属性进行设置

override var preferredStatusBarStyle: UIStatusBarStyle{get{ return .lightContent}};

效果如上图

提示框
let alert = UIAlertController.init(title: "提示", message: "我是弹出框", preferredStyle: .alert)

alert.addAction(UIAlertAction.init(title: "确定", style:.default, handler: nil));

alert.addAction(UIAlertAction.init(title: "取消", style: .cancel, handler: nil));

// 显示弹出
self.present(alert, animated: true) {   
}

显示效果:
这里写图片描述

补充:
当把代码中

   let alert = UIAlertController.init(title: "提示", message: "我是弹出框", preferredStyle: .alert)
   修改为
    let alert = UIAlertController.init(title: "提示", message: "我是弹出框", preferredStyle: .actionSheet)

其它保持不变,显示效果如图
这里写图片描述

全部移除子控件方法

需要循环遍历子控件,并从父控件中移除

方法1

view.subviews.map({ 
  $0.removeFromSuperview() 
})

方法2

for view in containerView.subviews{
    view.removeFromSuperview()
}
获取指定的storyboard

1.先创建一个storyboard,并命名为test.storyborad
2.拖一个view Controller到storyboard中,
并命名storyboard id=”test1”
3.代码如下

let storyboard=UIStoryboard(name: "test", bundle: nil);

// 获取StoreBoard中指定的View Controller
let viewTestController = storyboard.instantiateViewController(withIdentifier: "test1");

// 跳转到viewTestController页面
self.present(viewTestController, animated: true) {}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS Swift中连接扫描枪的代码主要分为两个部分:Bluetooth连接和接收扫描数据。 首先,需要在应用中引入CoreBluetooth库。在ViewController中创建一个BLEManager类,并像下面这样定义其属性和方法: ```swift class BLEManager: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate { var centralManager: CBCentralManager? var peripheral: CBPeripheral? var characteristic:CBCharacteristic? override init() { super.init() centralManager = CBCentralManager(delegate: self, queue: nil) } func centralManagerDidUpdateState(_ central: CBCentralManager) { if central.state == .poweredOn { central.scanForPeripherals(withServices:nil, options: nil) print("Scanning started") } else { print("Bluetooth not available.") } } func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) { if (peripheral.name == "Your peripheral name") { self.centralManager?.stopScan() self.peripheral = peripheral self.peripheral?.delegate = self self.centralManager?.connect(self.peripheral!, options: nil) } } func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) { if peripheral == self.peripheral { peripheral.discoverServices(nil) print("Connected to your peripheral") } } func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) { for service in peripheral.services! { peripheral.discoverCharacteristics(nil, for: service) } } func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) { for characteristic in service.characteristics! { if characteristic.uuid == CBUUID(string: "Your characteristic UUID") { self.characteristic = characteristic peripheral.setNotifyValue(true, for: characteristic) print("Characteristic found") } } } func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) { if characteristic == self.characteristic { if let data = characteristic.value { let string = String(data: data, encoding: .utf8)! print("Scanned barcode is: \(string)") } } } } ``` 接着,在ViewController中初始化BLEManager并调用centralManager的方法: ```swift var bleManager: BLEManager? override func viewDidLoad() { super.viewDidLoad() bleManager = BLEManager() } ``` 最后,需要将扫描枪的UUID和特征UUID替换为自己的。这样,应用就能连接扫描枪并接收扫描数据了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值