你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS Custom NavigationItem --寫titleView

IOS Custom NavigationItem --寫titleView

編輯:IOS開發綜合
        //先自己寫一個titleView
	UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 20)];//allocate titleView
	titleView.backgroundColor=[UIColor blackColor];
	//Create UILable
	UILabel	*titleText = [[UILabel alloc] initWithFrame: CGRectMake(100, 0, 50, 20)];//allocate titleText
	titleText.backgroundColor = [UIColor clearColor];
	[titleText setText:@"Title"];
	[titleView addSubview:titleText];
	[titleText release];//release titleText
	
	//Create Round UIButton
	UIButton *btnNormal = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	[btnNormal setFrame:CGRectMake(0, 0, 40, 20)];
	[btnNormal addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
	[btnNormal setTitle:@"Normal" forState:UIControlStateNormal];
	[btnNormal setFont:[UIFont systemFontOfSize:8]];
	[titleView addSubview:btnNormal];
	
	
	//Set to titleView
	 self.navigationItem.titleView = titleView;
	[titleView release];//release titleView
	
	//Custom backgroundImage UIButton
	UIButton *btnCustom = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	[btnCustom setFrame:CGRectMake(0, 0, 32, 32)];
	[btnCustom addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
	[btnCustom setTitle:@"CB" forState:UIControlStateNormal];
	[btnCustom setBackgroundImage:[UIImage imageNamed:@"whiteButton.png"] forState:UIControlStateNormal];
	[btnCustom setBackgroundImage:[UIImage imageNamed:@"blueButton.png"] forState:UIControlStateHighlighted];
	//Create UIBarButtonItem with the customed button
	UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:btnCustom];//allocate rightBarButton
	//Set to rightBarButtonItem
	self.navigationItem.rightBarButtonItem = rightBarButton;
	[rightBarButton release];//release rightBarButton
	

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved