Mal*_*oni 6 xcode uitabbarcontroller uitabbaritem ios
我正在一个应用程序中工作,我需要添加下划线UITabbarItem.
所以我想UITabbarItem在默认UITabbarcontroller的iOS 下添加下划线.
我已经创建了子类UITabbarcontroller但没有找到任何方法来添加行.
我想做像下图这样的事情.
如果有人对此有任何想法,请在这里分享.
小智 9
请试试这个.我已经在我的应用程序中使用了一次,并希望它也能帮到你.
这是我以编程方式创建Tab栏的方法:
UITabBarController *tab = [[UITabBarController alloc]init];
ViewController1 *v1 = [[ViewController1 alloc]init];
ViewController1 *v2 = [[ViewController1 alloc]init];
ViewController1 *v3 = [[ViewController1 alloc]init];
ViewController1 *v4 = [[ViewController1 alloc]init];
tab.viewControllers = [NSArray
arrayWithObjects:v1,v2,v3,v4,nil];
[[tab.tabBar.items objectAtIndex:0]setImage:[UIImage imageNamed:@""]];
[[tab.tabBar.items objectAtIndex:1]setImage:[UIImage imageNamed:@""]];
[[tab.tabBar.items objectAtIndex:2]setImage:[UIImage imageNamed:@""]];
[[tab.tabBar.items objectAtIndex:3]setImage:[UIImage imageNamed:@""]];
int divide = 4;
if([UIDevice currentDevice].userInterfaceIdiom ==UIUserInterfaceIdiomPad)
divide =6;
}
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(tab.tabBar.frame.origin.x,tab.tabBar.frame.origin.y, self.view.frame.size.width/divide, 56)];
UIImageView *border = [[UIImageView alloc]initWithFrame:CGRectMake(view.frame.origin.x,view.frame.size.height-6, self.view.frame.size.width/divide, 6)];
border.backgroundColor = “your color”;
[view addSubview:border];
[tab.tabBar setSelectionIndicatorImage:[self changeViewToImage:view]];
//This is the method that will draw the underline
-(UIImage ) changeViewToImage : (UIView ) viewForImage {
UIGraphicsBeginImageContext(viewForImage.bounds.size);
[viewForImage.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
Run Code Online (Sandbox Code Playgroud)
Swift代码3
在didFininshLaunch调用方法:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
UITabBar.appearance().selectionIndicatorImage = getImageWithColorPosition(color: UIColor.blue, size: CGSize(width:(self.window?.frame.size.width)!/4,height: 49), lineSize: CGSize(width:(self.window?.frame.size.width)!/4, height:2))
return true
}
Run Code Online (Sandbox Code Playgroud)
方法:
func getImageWithColorPosition(color: UIColor, size: CGSize, lineSize: CGSize) -> UIImage {
let rect = CGRect(x:0, y: 0, width: size.width, height: size.height)
let rectLine = CGRect(x:0, y:size.height-lineSize.height,width: lineSize.width,height: lineSize.height)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
UIColor.clear.setFill()
UIRectFill(rect)
color.setFill()
UIRectFill(rectLine)
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6192 次 |
| 最近记录: |