[英]How to load an Initial View Controller from Custom View controller class
在我的storyboard
我有一個FirstViewController
,它也是一個Initial View Controller
。 我的StoryBoard
有另一個名為BaseViewController
UIViewController
。 我在這上面添加了一個NavigationBar
。 我想從BaseViewController
派生出FirstViewController
,為此我編寫了以下代碼。
class FirstViewController: BaseViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
但是當我運行App時,它沒有顯示BaseViewController
的內容。 相反,它只加載FirstViewController
的內容。 我希望從BaseViewController
派生的每個UIViewControllere
都應該有它的NavigationBar
我需要更改才能得到它?
如果您的BaseViewController的目的只是為ViewControllers提供NavigationBar。 您可以將您的FirstViewController(NavController的rootcontroller)嵌入NavigationController(設置為初始視圖控制器)。 如果你想為不同的控制器使用不同的導航欄,你必須像我一樣以編程方式設置它。
看看這里 -
extension UIViewController {
func setNavBar(title : String, modType : ModuleType = .home, barHexColor : String? = nil, isBackBtn : Bool, isMenuBtn : Bool, isJummperBtn : Bool, isScreenshotBtn : Bool) {
k_ModuleType = modType
var barColor = "E14145"
var arrLeftItems = [UIBarButtonItem]()
var arrRightItems = [UIBarButtonItem]()
if let aBarColor = barHexColor {
barColor = aBarColor
}
self.navigationController?.navigationBar.isTranslucent = false
self.navigationController?.navigationBar.barTintColor = UIColor(hexString: barColor)
self.navigationController?.navigationBar.tintColor = UIColor.white
if isBackBtn {
let btnBack = UIButton(type: .custom)
btnBack.setImage(UIImage(named: "Back2"), for: .normal)
btnBack.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
btnBack.addTarget(self, action: #selector(self.barBackAction(_:)), for: .touchUpInside)
let itemBack = UIBarButtonItem(customView: btnBack)
arrLeftItems.append(itemBack)
}
if isMenuBtn {
let btnMenu = UIButton(type: .custom)
btnMenu.setImage(UIImage(named: "menu2"), for: .normal)
btnMenu.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
btnMenu.addTarget(self, action: #selector(self.barMenuAction(_:)), for: .touchUpInside)
let itemMenu = UIBarButtonItem(customView: btnMenu)
arrLeftItems.append(itemMenu)
}
if isScreenshotBtn {
let btnScreenshot = UIButton(type: .custom)
btnScreenshot.setImage(UIImage(named: "screenshot2"), for: .normal)
btnScreenshot.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
btnScreenshot.addTarget(self, action: #selector(self.barScreenshotAction(_:)), for: .touchUpInside)
let itemScreenshot = UIBarButtonItem(customView: btnScreenshot)
arrRightItems.append(itemScreenshot)
}
if isJummperBtn {
let btnJumpper = UIButton(type: .custom)
btnJumpper.setImage(UIImage(named: "jumpper2"), for: .normal)
btnJumpper.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
btnJumpper.addTarget(self, action: #selector(self.barJumpperAction(_:)), for: .touchUpInside)
let itemJumpper = UIBarButtonItem(customView: btnJumpper)
arrRightItems.append(itemJumpper)
}
self.navigationItem.setLeftBarButtonItems(arrLeftItems, animated: true)
self.navigationItem.setRightBarButtonItems(arrRightItems, animated: true)
let lblNavTitle = UILabel(frame: CGRect(x: 0, y: 0,
width: DeviceType.isIPad() ? 200 : 110,
height: 40))
lblNavTitle.textAlignment = .center
lblNavTitle.numberOfLines = 0
lblNavTitle.textColor = UIColor.white
lblNavTitle.text = title
lblNavTitle.font = UIFont.systemFont(ofSize: DeviceType.isIPad() ? 21 : 16,
weight: .medium)
lblNavTitle.backgroundColor = UIColor.clear
self.navigationItem.titleView = lblNavTitle
self.navigationItem.titleView?.isUserInteractionEnabled = true
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.tapTitle(_:)))
tapGesture.numberOfTapsRequired = 1
self.navigationItem.titleView?.addGestureRecognizer(tapGesture)
}
然后在FirstViewController的Viewdidload()方法中
override func viewdidload()
{
super.viewdidload()
self.setNavBar(title : aTitle,
barHexColor : nil,
isBackBtn : false,
isMenuBtn : true,
isJummperBtn : true,
isScreenshotBtn : true)
}
@interface CommonViewController ()<UITextFieldDelegate>//,UITableViewDelegate,UITableViewDataSource>
{
APIFactory *fireAPI;
Supportive *support;
MBProgressHUD *HUD;
BOOL blinkStatus;
}
@end
@implementation CommonViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(UIStatusBarStyle)preferredStatusBarStyle{
return UIStatusBarStyleLightContent;
}
- (void)viewDidLoad
{
[super viewDidLoad];
fireAPI = [[APIFactory alloc] init];
support = [[Supportive alloc] init];
self.dbManager = [[DBManager alloc] initWithDatabaseFilename:@"IntelliDealer.sqlite"];
self.dataArray = [[NSMutableArray alloc]init];
self.strVC=@"OrderViewController";
[self setNavigationBar];
if (self.epcCartOrder.count==0) {
lblBadge.hidden = YES;
badgeImgView.hidden = YES;
}
else{
lblBadge.hidden = NO;
badgeImgView.hidden = NO;
}
}
-(void)setNavigationBar{
NSString *query1;
query1 =[NSString stringWithFormat:@"SELECT * FROM Dealer_Spare_Order WHERE spareOrderListId=%d ORDER BY spareAddTime DESC",[[[UICKeyChainStore keyChainStore] stringForKey:@"DealerEnableOrderList"] intValue]];
self.epcCartOrder = [[NSMutableArray alloc] initWithArray:[self.dbManager loadDataFromDB:query1]];
[Supportive ShowLog:[NSString stringWithFormat:@"no of rows => %lu",(unsigned long)self.epcCartOrder.count]];
self.navigationController.navigationBarHidden = YES;
CustomTitleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 768, iPhoneX ? 100:70)];
CustomTitleView.backgroundColor = [UIColor colorWithRed:231/255.f green:66/255.f blue:57/255.f alpha:1.0f];
backButton =[UIButton buttonWithType:UIButtonTypeCustom];
[backButton addTarget:self action:@selector(didclickOnBackButton) forControlEvents:UIControlEventTouchUpInside];
backButtonImage = [UIImageView new];
menuButton =[UIButton buttonWithType:UIButtonTypeCustom];
menuButton.tag = 100;
[menuButton addTarget:self action:@selector(didclickOnMenuButton) forControlEvents:UIControlEventTouchUpInside];
lblDealerName = [[UILabel alloc] init];
lblDealerName.numberOfLines=3;
lblDealerName.text= [[UICKeyChainStore keyChainStore] stringForKey:@"DealerName"];
lblDealerName.textAlignment=NSTextAlignmentCenter;
lblDealerName.textColor = [UIColor whiteColor];
lblDealerName.backgroundColor=[UIColor clearColor];
lblDealerName.userInteractionEnabled=NO;
btnDealerName =[UIButton buttonWithType:UIButtonTypeCustom];
[btnDealerName addTarget:self action:@selector(didclickOnDealerName) forControlEvents:UIControlEventTouchUpInside];
[btnDealerName addSubview:lblDealerName];
addToCartButton =[UIButton buttonWithType:UIButtonTypeCustom];
[addToCartButton addTarget:self action:@selector(didclickOnAddToCartButtonButton) forControlEvents:UIControlEventTouchUpInside];
addToCartButtonImage = [UIImageView new];
[self.view addSubview:CustomTitleView];
[CustomTitleView addSubview:backButtonImage];
[CustomTitleView addSubview:backButton];
[CustomTitleView addSubview:menuButton];
[CustomTitleView addSubview:btnDealerName];
//[CustomTitleView addSubview:cartButton];
[CustomTitleView addSubview:addToCartButtonImage];
[CustomTitleView addSubview:addToCartButton];
if(UI_USER_INTERFACE_IDIOM () == UIUserInterfaceIdiomPad) {
[backButton setFrame:CGRectMake(0, 8, 45, 60)];
// [backButton setImage:[UIImage imageNamed:@"back_arrow"] forState:UIControlStateNormal];
[backButtonImage setFrame:CGRectMake(0, 18, 40, 40)];
[backButtonImage setImage:[UIImage imageNamed:@"back_arrow"]];
[menuButton setFrame:CGRectMake(50, 18, 40, 40)];
[menuButton setImage:[UIImage imageNamed:@"ipadnavigatio"] forState:UIControlStateNormal];
[btnDealerName setFrame:CGRectMake(100, 24, 590, 30)];
[lblDealerName setFrame:CGRectMake(0, 0, 590, 30)];
[lblDealerName setTextAlignment:NSTextAlignmentCenter];
lblDealerName.font = [UIFont fontWithName:@"Arial" size:22];
[addToCartButton setFrame:CGRectMake(690, 10, 70, 60)];
[addToCartButtonImage setFrame:CGRectMake(700, 23, 40, 38)];
[addToCartButtonImage setImage:[UIImage imageNamed:@"ipadaddtocart"]];
badgeImgView = [[UIImageView alloc]init];
//badgeImgView.hidden =YES;
badgeImgView.image = [UIImage imageNamed:@"order_bg"];
lblBadge = [[UILabel alloc] init];
if (self.epcCartOrder.count<10) {
badgeImgView.frame =CGRectMake(36,9, 23, 23);
lblBadge.frame = CGRectMake(7, 0, 23, 23);
}
else{
badgeImgView.frame =CGRectMake(38,9, 25, 25);
lblBadge.frame = CGRectMake(4, 0, 25, 25);
}
/*important--------- */
lblBadge.textColor = [UIColor whiteColor];
lblBadge.font = [UIFont fontWithName:@"Arial" size:15];
lblBadge.backgroundColor=[UIColor clearColor];
lblBadge.textColor=[UIColor whiteColor];
lblBadge.userInteractionEnabled=NO;
//lblBadge.hidden = YES;
lblBadge.text=[NSString stringWithFormat:@"%lu", (unsigned long)self.epcCartOrder.count];
//[CustomTitleView addSubview:_textFieldSearch];
[addToCartButtonImage addSubview: badgeImgView];
[badgeImgView addSubview:lblBadge];
[addToCartButton addSubview: badgeImgView];
}
else if (UI_USER_INTERFACE_IDIOM () == UIUserInterfaceIdiomPhone){
double customViewWidth = self.view.frame.size.width - 40;
[backButton setFrame:CGRectMake(5, iPhoneX ? 20:10, 40, 60)];
// [backButton setImage:[UIImage imageNamed:@"back_arrow"] forState:UIControlStateNormal];
[backButtonImage setFrame:CGRectMake(10, iPhoneX ? 55:25, 35, 35)];
[backButtonImage setImage:[UIImage imageNamed:@"back_arrow"]];
[menuButton setFrame:CGRectMake(50, iPhoneX ? 55:25, 35, 35)];
[menuButton setImage:[UIImage imageNamed:@"ipadnavigatio.png"] forState:UIControlStateNormal];
[btnDealerName setFrame:CGRectMake(90, iPhoneX ? 50:20, 180, 51)];
[lblDealerName setFrame:CGRectMake(0, 0, 180, 51)];
lblDealerName.font = [UIFont fontWithName:@"Arial" size:15];
[addToCartButton setFrame:CGRectMake(customViewWidth - 8, iPhoneX ? 55:15, 50, 55)];
[addToCartButtonImage setFrame:CGRectMake(customViewWidth , iPhoneX ? 60:30, 30, 30)];
[addToCartButtonImage setImage:[UIImage imageNamed:@"ipadaddtocart"]];
badgeImgView = [[UIImageView alloc]init];
badgeImgView.image = [UIImage imageNamed:@"order_bg"];
lblBadge = [[UILabel alloc] init];
lblBadge.textColor = [UIColor whiteColor];
lblBadge.font = [UIFont fontWithName:@"Arial" size:15];
lblBadge.backgroundColor=[UIColor clearColor];
lblBadge.textColor=[UIColor whiteColor];
lblBadge.userInteractionEnabled=NO;
lblBadge.text=[NSString stringWithFormat:@"%lu", (unsigned long)self.epcCartOrder.count];
[addToCartButtonImage addSubview: badgeImgView];
[badgeImgView addSubview:lblBadge];
[addToCartButton addSubview: badgeImgView];
if (self.epcCartOrder.count<10) {
badgeImgView.frame=CGRectMake(28, iPhoneX ? 1:9, 20, 20);
lblBadge.frame = CGRectMake(6, 0, 20, 20);
}
else{
badgeImgView.frame=CGRectMake(28, 6, 22, 22);
lblBadge.frame = CGRectMake(2, 0, 22, 22);
}
//lblBadge.center = badgeImgView.center;
/*important--------- */
// [CustomTitleView addSubview:_textFieldSearch];
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.