我试图在AppBar中居中对齐徽标。真的卡住了,实现了徽标单独在中心。垂直和水平。
这是我的AppBar.tsx
import * as React from 'react';
import { styled, useTheme } from '@mui/material/styles';
import MuiAppBar, { AppBarProps as MuiAppBarProps } from '@mui/material/AppBar';
import Toolbar from '@mui/material/Toolbar';
import Typography from '@mui/material/Typography';
import IconButton from '@mui/material/IconButton';
import MenuIcon from '@mui/icons-material/Menu';
import logo from './../assets/images/logo_white.svg'
import Box from '@mui/material/Box';
import MoreIcon from '@mui/icons-material/MoreVert';
const drawerWidth = 240;
interface AppBarProps extends MuiAppBarProps {
open?: boolean;
}
const AppBar = styled(MuiAppBar, {
shouldForwardProp: (prop) => prop !== "open"
})<AppBarProps>(({ theme, open }) => ({
transition: theme.transitions.create(["margin", "width"], {
easing: theme.transitions.easing.sharp,
duration: theme.transitions.duration.leavingScreen
}),
...(open && {
width: `calc(100% - ${drawerWidth}px)`,
marginLeft: `${drawerWidth}px`,
transition: theme.transitions.create(["margin", "width"], {
easing: theme.transitions.easing.easeOut,
duration: theme.transitions.duration.enteringScreen
})
})
}));
export default function AppBar({ open, onDrawerOpen }:any) {
const theme = useTheme();
return (
<AppBar position="fixed" style={{ background: "#002a5e" }} open={open}>
<Toolbar>
<IconButton
color="inherit"
aria-label="open drawer"
onClick={onDrawerOpen}
edge="start"
sx={{ mr: 2, ...(open && { display: "none" }) }}
>
<MenuIcon />
</IconButton>
<Typography variant="h6" noWrap component="div">
TITLE
</Typography>
<Box
component="img"
sx={{
height: 32,
}}
alt="MyLogo"
src={logo}
/>
</Toolbar>
</AppBar>
);
}
现在它只是显示与标题文本。请帮助
2条答案
按热度按时间dgiusagp1#
有许多可能的方法,作为一个基本的例子,也许尝试添加一个容器与
flex: 1
的两侧的标志。这应该总是把标志推到
Toolbar
的中心,但仍然保持按钮和标题在它的开始,因为它似乎是理想的结果。示例:
m2xkgtsf2#
您需要将
IconButton
和Typography
打包到一个Box
中,并将其display
设置为absolute
。然后您可以使用flex属性管理徽标。This是一个很好的资源,可以学习您可以采取的所有方法。