ignoresSafeArea:

機(jī)翻:regions:從安全區(qū)域中刪除的應(yīng)忽略的矩形類型(即添加回新子視圖的安全區(qū)域)。
edges:視圖的邊緣可能是起點(diǎn),任何不在此集合中的邊緣都將保持不變,即使該邊緣鄰接區(qū)域中列出的安全區(qū)域。
看起來(lái)在SwiftUI中,ignoresSafeArea作用是可以讓view忽略安全邊距(比如全面屏手機(jī)頂部和底部),然后經(jīng)過(guò)代碼測(cè)試,發(fā)現(xiàn)ignoresSafeArea有以下特點(diǎn):
1:如果view是在navgation內(nèi),不設(shè)置ignoresSafeArea則navgation的頂部也會(huì)算作安全距離。
2:以以下view為例
var resetConfirm: some View {
VStack(spacing: 0) {···}
.background(Color.white) //背景色
.clipShape(CustomCorner(corners: [.topLeft, .topRight], radius: 30)) //指定圓角
.frame(maxHeight: .infinity, alignment: .bottom) //view高度為最大,貼底邊
.border(Color.yellow) // 黃色邊框
//SafeArea1
//.ignoresSafeArea(edges: [.all])
.background(Color.gray)
//SafeArea2
.ignoresSafeArea(edges: [.all])
.border(Color.black) // 黑色邊框
}
}
當(dāng)SafeArea1設(shè)置ignoresSafeArea,在SafeArea2把ignoresSafeArea注釋掉,會(huì)發(fā)現(xiàn)黃色邊框是全屏的,但是background不是全屏的,是有安全邊距的,即:全屏view.background(),并不會(huì)得到全屏的背景。
然后在SafeArea2設(shè)置ignoresSafeArea,在SafeArea1把ignoresSafeArea注釋掉。
會(huì)發(fā)現(xiàn)background是全屏了,但是黑色邊框依舊會(huì)有安全邊距
由此可以推測(cè):
ignoresSafeArea修飾一個(gè)view時(shí),view會(huì)忽略安全邊距,但是在之后view的疊加,安全邊距默認(rèn)依舊是有的。
推測(cè):swiftUI中,.board/.background等方法都屬于以下方法
func xxxxx() -> some View
而這個(gè)some View都會(huì)以開(kāi)啟安全邊距來(lái)繪制。除非設(shè)置ignoresSafeArea來(lái)忽略安全邊距。
而SwiftUI繪制順序似乎是由外到內(nèi)的,所以當(dāng)最外層設(shè)置了ignoresSafeArea,內(nèi)部的view都會(huì)忽略掉安全邊距,但是內(nèi)部view設(shè)置ignoresSafeArea并不會(huì)對(duì)外層產(chǎn)生影響。只會(huì)影響自身及更內(nèi)層的view。