2015-09-19 7 views
5

में अंडरलाइन कैसे जोड़ें मैं एक ऐसे एप्लिकेशन में काम कर रहा हूं जहां मुझे UITabbarItem में अंडरलाइन जोड़ने की आवश्यकता है।आईओएस: UITabBarItem

इसलिए मैं चयनित UITabbarItem के तहत आईओएस के डिफ़ॉल्ट UITabbarcontroller में अंडरलाइन जोड़ना चाहता हूं।

मैंने पहले ही UITabbarcontroller का उप-वर्ग बनाया है लेकिन उसमें लाइन जोड़ने का कोई तरीका नहीं मिला है।

मैं नीचे छवि की तरह कुछ करना चाहता हूं।

UITabbarWithUnderline Image

अगर कोई किसी भी विचार है इस यहाँ साझा करें के लिए।

उत्तर

9

यह एक प्रयास करें। मैंने एक बार अपने आवेदन में उपयोग किया है और उम्मीद है कि यह आपकी भी मदद करेगा।

 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; 
} 
+0

यह कोड आईपैड के लिए भी काम करता है ... (y) –

-1

इस कोड का प्रयोग करें किसी भी UIView ऑब्जेक्ट में एक निचली सीमा को जोड़ने के लिए, बस अपने UITabbarItem वस्तु के साथ view बदल देते हैं:

CALayer *bottomBorder = [CALayer layer]; 
bottomBorder.frame = CGRectMake(0, view.frame.size.height - 1, view.frame.size.width - 1, 1); 
bottomBorder.backgroundColor = [UIColor lightGrayColor].CGColor; 
[view.layer addSublayer:bottomBorder]; 
+0

आप इस समाधान अपने आप को चेक किया? 'UITabBarItem' को' UIView' से प्राप्त नहीं होता है और इसमें 'परत' संपत्ति नहीं होती है। – Eimantas

4

बस AppDelegate में यह कोड:

यह कैसे मैं प्रोग्राम के रूप में टैब बार बनाया है है।

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
// Override point for customization after application launch. 

UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake([UITabBar appearance].frame.origin.x,[UITabBar appearance].frame.origin.y, [[UIScreen mainScreen] bounds].size.width/3, 56)]; 

UIImageView *border = [[UIImageView alloc]initWithFrame:CGRectMake(view1.frame.origin.x,view1.frame.size.height-6, [[UIScreen mainScreen] bounds].size.width/3, 6)]; 
border.backgroundColor = [UIColor colorWithRed:255.0f/255.0f green:105.0f/255.0f blue:84.0f/255.0f alpha:1.0f]; 
[view1 addSubview:border]; 
UIImage *img=[self ChangeViewToImage:view1]; 
[[UITabBar appearance] setSelectionIndicatorImage:img]; 
[[UITabBar appearance] setTintColor: [UIColor colorWithRed:255.0f/255.0f green:105.0f/255.0f blue:84.0f/255.0f alpha:1.0f]]; 

return YES; 
} 
-(UIImage *) ChangeViewToImage : (UIView *) viewForImage 
{ 
UIGraphicsBeginImageContext(viewForImage.bounds.size); 
[viewForImage.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
return img; 
} 
4

AppDelegate didFininshLaunch विधि जोड़ें वैश्विक शैली में UITabBar

के लिए
UITabBar.appearance().selectionIndicatorImage = UIImage.getImageWithColorPosition(UIColor.whiteColor(), size: CGSizeMake(kScreenWidth/4, 49), lineSize: CGSizeMake(kScreenWidth/4, 2)) 

class func getImageWithColorPosition(color: UIColor, size: CGSize, lineSize: CGSize) -> UIImage { 
    let rect = CGRectMake(0, 0, size.width, size.height) 
    let rectLine = CGRectMake(0, size.height-lineSize.height, lineSize.width, lineSize.height) 
    UIGraphicsBeginImageContextWithOptions(size, false, 0) 
    UIColor.clearColor().setFill() 
    UIRectFill(rect) 
    color.setFill() 
    UIRectFill(rectLine) 
    let image: UIImage = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 
    return image 
} 

संपादित स्विफ्ट 3

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 = UIGraphicsGetImageFromCurrentImageContext()! 
    UIGraphicsEndImageContext() 
    return image 
} 
5

स्विफ्ट 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 
    } 

विधि:

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 
    } 
संबंधित मुद्दे