1. 如何去除Today擴(kuò)展左邊的margin
Paste_Image.png
如上圖去除紅色邊框。
在Today的這個(gè)類中,重寫下面這個(gè)方法:
func widgetMarginInsetsForProposedMarginInsets(defaultMarginInsets: UIEdgeInsets) -> UIEdgeInsets {
return UIEdgeInsetsZero
}
2. 單獨(dú)測試Today擴(kuò)展
Paste_Image.png
將target換成擴(kuò)展再運(yùn)行。
3. Today擴(kuò)展獲取地理位置
- 導(dǎo)入CoreLocation框架
import CoreLocation
- 修改Today的info
添加NSLocationWhenInUseUsageDescription字段在info中。
Paste_Image.png - 在Today類viewDidLoad添加如下代碼
var locationManager: CLLocationManager = CLLocationManager()
locationManager.delegate = self
if locationManager.respondsToSelector("requestWhenInUseAuthorization") {
//這個(gè)方法是當(dāng)用戶允許定位之后就立刻響應(yīng)的
locationManager.requestWhenInUseAuthorization()
}
locationManager.startUpdatingLocation()
- 添加回掉方法
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location: CLLocation = locations.last!
city.text = "\(location.coordinate)"
}