iOS OC 设置self.tabBar圆角
时间: 2024-06-15 15:07:21 浏览: 14
要设置`self.tabBar`的圆角,你可以按照以下步骤进行操作:
1. 在你的视图控制器类中,找到`viewDidLoad`方法或者其他适合的位置。
2. 使用`self.tabBar.layer.cornerRadius`属性来设置`self.tabBar`的圆角半径。例如,你可以将其设置为10来创建一个圆角为10的效果。
3. 为了确保圆角效果生效,还需要将`self.tabBar.layer.masksToBounds`属性设置为`true`,以剪切超出圆角范围的内容。
下面是一个示例代码:
```objective-c
- (void)viewDidLoad {
[super viewDidLoad];
// 设置圆角半径
self.tabBar.layer.cornerRadius = 10;
// 剪切超出圆角范围的内容
self.tabBar.layer.masksToBounds = YES;
}
```
这样,你就可以在iOS的Objective-C中设置`self.tabBar`的圆角了。
相关问题
iOS 使用OC自定义tabbar
在 iOS 中,可以使用 Objective-C 自定义 TabBar。下面是一个简单的步骤:
1. 创建一个继承自 UITabBarController 的类,例如 MyTabBarController。
2. 在 MyTabBarController.m 中,实现 viewDidLoad 方法,在其中创建自定义 TabBar。
```objc
- (void)viewDidLoad {
[super viewDidLoad];
// 创建自定义 TabBar
MyTabBar *myTabBar = [[MyTabBar alloc] init];
myTabBar.delegate = self;
[self setValue:myTabBar forKey:@"tabBar"];
}
```
3. 在 MyTabBarController.m 中,实现 UITabBarDelegate 协议中的方法,例如 tabBar:didSelectItem:,处理 TabBar 点击事件。
```objc
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
// 处理 TabBar 点击事件
}
```
4. 创建一个继承自 UITabBar 的类,例如 MyTabBar。
5. 在 MyTabBar.m 中,实现 layoutSubviews 方法,在其中布局 TabBar。
```objc
- (void)layoutSubviews {
[super layoutSubviews];
// 布局 TabBar
}
```
6. 在 MyTabBar.m 中,实现 drawRect: 方法,在其中绘制 TabBar。
```objc
- (void)drawRect:(CGRect)rect {
// 绘制 TabBar
}
```
7. 在 MyTabBar.m 中,实现 touchUpInside 方法,在其中处理 TabBar 点击事件。
```objc
- (void)touchUpInside:(UIButton *)button {
// 处理 TabBar 点击事件
}
```
8. 在 MyTabBar.m 中,添加 TabBar 上的按钮,例如:
```objc
- (void)layoutSubviews {
[super layoutSubviews];
// 添加 TabBar 上的按钮
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(0, 0, self.frame.size.width / 4, self.frame.size.height);
[button1 addTarget:self action:@selector(touchUpInside:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button1];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
button2.frame = CGRectMake(self.frame.size.width / 4, 0, self.frame.size.width / 4, self.frame.size.height);
[button2 addTarget:self action:@selector(touchUpInside:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button2];
UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
button3.frame = CGRectMake(self.frame.size.width / 2, 0, self.frame.size.width / 4, self.frame.size.height);
[button3 addTarget:self action:@selector(touchUpInside:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button3];
UIButton *button4 = [UIButton buttonWithType:UIButtonTypeCustom];
button4.frame = CGRectMake(self.frame.size.width * 3 / 4, 0, self.frame.size.width / 4, self.frame.size.height);
[button4 addTarget:self action:@selector(touchUpInside:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button4];
}
```
9. 在 MyTabBar.m 中,重写 setItems:animated: 方法,在其中设置 TabBar 上按钮的图标和标题。
```objc
- (void)setItems:(NSArray<UITabBarItem *> *)items animated:(BOOL)animated {
[super setItems:items animated:animated];
// 设置 TabBar 上按钮的图标和标题
for (int i = 0; i < items.count; i++) {
UITabBarItem *item = items[i];
UIButton *button = self.subviews[i + 1];
[button setImage:item.image forState:UIControlStateNormal];
[button setTitle:item.title forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}
}
```
以上就是使用 Objective-C 自定义 TabBar 的简单步骤。当然,还可以根据需求进行更详细的定制。
将下面swift的代码翻译成oc. if #available(iOS 15.0, *) { let appearnce = UITabBarAppearance() appearnce.configureWithOpaqueBackground() appearnce.backgroundColor = .clearColor tabBarController.tabBar.standardAppearance = appearnce tabBarController.tabBar.scrollEdgeAppearance = appearnce }
if (score > 90) {
print("优秀")
} else if (score > 60) {
print("及格")
} else {
print("不及格")
}
如果 (score > 90) {
NSLog(@"优秀");
} else if (score > 60) {
NSLog(@"及格");
} else {
NSLog(@"不及格");
}
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)