UIButton中setTitleEdgeInsets和setImageEdgeInsets的使用

本文详细介绍了UIButton内部titleLabel和imageView的布局调整方法,包括不同情况下的显示规则及如何通过设置EdgeInsets来精确控制图片和文本的位置。

 UIButton内有两个控件titleLabel和imageView,可以用来显示一个文本和图片,这里的图片区别于背景图片。给UIButton设置了title和image后,它们会图片在左边,文本在图片右边显示。它们两个做为一个整体依赖于button的contentHorizontalAlignment居左居右或居中显示。

        1.当button.width < image.width时,只显示被压缩后的图片,图片是按fillXY的方式压缩。

        2.当button.width > image.width,且 button.width < (image.width + text.width)时,图片正常显示,文本被压缩。

        3.当button.width > (image.width + text.width),两者并列默认居中显示,可通过button的属性contentHorizontalAlignment改变对齐方式。

4.想两改变两个子控件的显示位置,可以分别通过setTitleEdgeInsets和setImageEdgeInsets来实现。需要注意的是,对titleLabel和imageView设置偏移,是针对它当前的位置起作用的,并不是针对它距离button边框的距离的。我测试下来,当button的contentHorizontalAlignment为居中时,偏移的距离和实际传的值有些偏差,没有发现规律,看不到源码也没在研究,但把button的contentHorizontalAlignment设为居左时,contentVerticalAlignment设为居上时,可以很方便的通过EdgeInsets改变两个子控件的位置。居左时,运行结果如图1.

前提:UIButton: width=220, height=100,  image: width=height=36    text width=62

想要让图片和文本上下排列,需要让image向下偏移10(距离上边间隙),然后向右偏移92( button.width - image.width / 2),计算下来为 [btn setImageEdgeInsets:UIEdgeInsetsMake(59200)],它的偏移是针对它图1时的位置,如果想向上偏移传的为负值。

下面要计算机文本偏移了,向下偏移46 (36+10 图片的高度+间隙),向右偏移44 ( (button.width - text.width) / 2  -  image.width );因为文本本身起始x方向位置是从image.width开始的,所以算偏移时,要减掉这个宽度。计算结果为[btn setTitleEdgeInsets:UIEdgeInsetsMake(464400)];



  图1




    图2

使用 Objective-C 自定义 `UITabBarController` 的 `UITabBar` 时,通常需要继承 `UITabBarController` 并对其 `tabBar` 属性进行定制。以下是一个完整的示例代码,展示了如何创建一个自定义的 `UITabBarController` 子类,并在其中调整 `UITabBar` 的外观行为。 ### 自定义 UITabBarController 子类 首先,创建一个 `UITabBarController` 的子类,例如 `CustomTabBarController`。 #### CustomTabBarController.h ```objc #import <UIKit/UIKit.h> @interface CustomTabBarController : UITabBarController @end ``` #### CustomTabBarController.m ```objc #import "CustomTabBarController.h" @implementation CustomTabBarController - (void)viewDidLoad { [super viewDidLoad]; // 自定义 UITabBar 的外观 self.tabBar.barTintColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:1.0]; // 设置背景颜色 self.tabBar.tintColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0]; // 设置选中项的颜色 // 创建视图控制器并添加到 tab bar controller 中 UIViewController *vc1 = [[UIViewController alloc] init]; vc1.view.backgroundColor = [UIColor whiteColor]; vc1.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:0]; UIViewController *vc2 = [[UIViewController alloc] init]; vc2.view.backgroundColor = [UIColor lightGrayColor]; vc2.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMore tag:1]; self.viewControllers = @[vc1, vc2]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; // 可以在这里进一步自定义 tabBar 的布局或行为 for (UIView *subview in self.tabBar.subviews) { if ([subview isKindOfClass:[UIButton class]]) { UIButton *button = (UIButton *)subview; [button setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, -10, 0)]; // 调整文字位置 [button setImageEdgeInsets:UIEdgeInsetsMake(-10, 0, 0, 0)]; // 调整图标位置 } } } @end ``` ### 在 AppDelegate 或其他地方使用自定义 TabBarController 在 `AppDelegate` 中设置 `CustomTabBarController` 作为根视图控制器。 #### AppDelegate.m ```objc #import "AppDelegate.h" #import "CustomTabBarController.h" @interface AppDelegate () @property (strong, nonatomic) UIWindow *window; @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; CustomTabBarController *customTabBarController = [[CustomTabBarController alloc] init]; self.window.rootViewController = customTabBarController; [self.window makeKeyAndVisible]; return YES; } @end ``` 通过上述代码,可以实现对 `UITabBarController` 其 `UITabBar` 的基本自定义,包括背景颜色、选中项颜色以及图标文字的位置调整。这种实现方式允许开发者根据具体的业务需求灵活调整界面样式交互逻辑[^2]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值