UITabBarItem 更改图像的高度和宽度/UINavigationBar 更改样式

本文详细介绍了如何在iOS应用中自定义TabBar图标尺寸、使用imageInset调整图片位置,并通过设置导航栏外观来实现个性化UI设计。通过调整imageInset属性和拉伸图像,可以实现TabBar图标在不同屏幕分辨率下的适配,同时通过设置阴影、字体颜色等属性,可以进一步美化导航栏。此外,文章还展示了如何为导航栏添加左按钮,并提供了调整图片大小和控件一致的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

从苹果文档的最大大小的一张图片在 TabBar 是 30 x 30 (60 x 60 的视网膜显示器)。

我不认为它是可能采取整个 TabBar 不拉伸图像的高度。我认为最好的解决办法是中心中使用 imageInset TabBar 的图像

tabBarItem1.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);

enter image description here

否则你可以玩这 imageInset 和拉伸图像像在截图中

tabBarItem1.imageInsets = UIEdgeInsetsMake(0, -10, -6, -10);

enter image description here

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  {


 UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;

 UITabBar *tabBar = tabBarController.tabBar;
 UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
 UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
 UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];

 //add image to tabbarItems

 tabBarItem1.imageInsets = UIEdgeInsetsMake(0, -10, -6, -10);
}

 [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];

    NSShadow *shadow = [[NSShadow alloc] init];

    shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];

    shadow.shadowOffset = CGSizeMake(0, 1);

    // [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0]

    [[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:

                                                           [UIColor grayColor], NSForegroundColorAttributeName,

                                                           shadow, NSShadowAttributeName,

                                                           [UIFont fontWithName:@"Avenir-Medium"  size:20.0], NSFontAttributeName, nil]];


 UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc] initWithImage:[self scaleImageToSize:[UIImage imageNamed:@"ico_location_up"size:CGSizeMake(20, 26)] style:UIBarButtonItemStyleBordered target:self action:@selector(do)];

    self.navigationItem.leftBarButtonItem = cameraItem;


//修改图片大小和控件一致

- (UIImage *)scaleImageToSize:(UIImage *)img size:(CGSize)size

{

    UIGraphicsBeginImageContext(size);

    [img drawInRect:CGRectMake(0, 0, size.width, size.height)];

    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return scaledImage;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值