*注:由于膠囊按鈕是原生組件,為表現(xiàn)一直,其單位在個(gè)系統(tǒng)都為px,所以我們的自定義導(dǎo)航欄各個(gè)高度的單位都必需是px(切記不能用rpx),才能完美適配。
3、解決問(wèn)題
通過(guò)上述分析,相信小伙伴們都能有一個(gè)解決問(wèn)題的思路了,在上代碼之前,小灰再給大家畫(huà)一下重點(diǎn):
話不多說(shuō),上代碼(gitHub地址):
js:
Component({ properties: { background: { type: String, value: 'rgba(255, 255, 255, 1)' }, color: { type: String, value: 'rgba(0, 0, 0, 1)' }, titleText: { type: String, value: '導(dǎo)航欄' }, titleImg: { type: String, value: '' }, backIcon: { type: String, value: '' }, homeIcon: { type: String, value: '' }, fontSize: { type: Number, value: 16 }, iconHeight: { type: Number, value: 19 }, iconWidth: { type:Number, value: 58 } }, attached: function(){ var that = this; that.setNavSize(); that.setStyle(); }, data: { }, methods: { // 通過(guò)獲取系統(tǒng)信息計(jì)算導(dǎo)航欄高度 setNavSize: function() { var that = this , sysinfo = wx.getSystemInfoSync() , statusHeight = sysinfo.statusBarHeight , isiOS = sysinfo.system.indexOf('iOS') > -1 , navHeight; if (!isiOS) { navHeight = 48; } else { navHeight = 44; } that.setData({ status: statusHeight, navHeight: navHeight }) }, setStyle: function() { var that = this , containerStyle , textStyle , iconStyle; containerStyle = [ 'background:' + that.data.background ].join(';'); textStyle = [ 'color:' + that.data.color, 'font-size:' + that.data.fontSize + 'px' ].join(';'); iconStyle = [ 'width: ' + that.data.iconWidth + 'px', 'height: ' + that.data.iconHeight + 'px' ].join(';'); that.setData({ containerStyle: containerStyle, textStyle: textStyle, iconStyle: iconStyle }) }, // 返回事件 back: function(){ wx.navigateBack({ delta: 1 }) this.triggerEvent('back', {back: 1}) }, home: function() { this.triggerEvent('home', {}); } }})
wxml:
<view class='nav' style='height: {{status + navHeight}}px'> <view class='status' style='height: {{status}}px;{{containerStyle}}'></view> <view class='navbar' style='height:{{navHeight}}px;{{containerStyle}}'> <view class='back-icon' wx:if="{{backIcon}}" bindtap='back'> <image src='{{backIcon}}'></image> </view> <view class='home-icon' wx:if="{{homeIcon}}" bindtap='home'> <image src='{{homeIcon}}'></image> </view> [鏈接描述][10] <view class='nav-icon' wx:if="{{titleImg}}"> <image src='{{titleImg}}' style='{{iconStyle}}'></image> </view> <view class='nav-title' wx:if="{{titleText && !titleImg}}"> <text style='{{textStyle}}'>{{titleText}}</text> </view> </view> </view>
wxss:
.navbar{ position: relative } .back-icon, .home-icon{ width: 28px; height: 100%; position: absolute; transform: translateY(-50%); top: 50%; display: flex; } .back-icon{ left: 16px; } .home-icon{ left: 44px } .back-icon image{ width: 28px; height: 28px; margin: auto; } .home-icon image{ width: 20px; height: 20px; margin: auto; } .nav-title, .nav-icon{ position: absolute; transform: translate(-50%, -50%); left: 50%; top: 50%; font-size: 0; font-weight: bold; }
運(yùn)行效果圖:
文字標(biāo)題:
圖片標(biāo)題:
4、總結(jié)
經(jīng)過(guò)小灰的一番論證以及實(shí)踐經(jīng)驗(yàn),最終總結(jié)出以上最終解決方案,但希望對(duì)小伙伴們有所幫助,如果小伙伴們覺(jué)得有用,記得給顆star哦 --> 點(diǎn)我,后續(xù)還會(huì)更新其他組件。
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com