最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]Swift代码方式的去创建一个基于tab页面的app

Swift crifan 2666浏览 0评论

[背景]

发现自带的,都不是我想要的。
连官网都是:
Start Developing iOS Apps (Swift): Build a Basic UI
基于界面工具的。
[解决过程]
1.搜:

swift programmatically create new project tab page
参考:
2.搜:
swift programmatically create Tabbar
最后暂时是:
AppDelegate.swift
相关代码:
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.

        //application.statusBarHidden = true;

        //init main view controller
        let mainViewVC = MainViewController()
        let navigation = UINavigationController(rootViewController:mainViewVC)

        navigation.view.layer.shadowOffset = CGSizeMake(0,0);
        navigation.view.layer.shadowOpacity = 0.5;
        navigation.view.layer.shadowRadius = 10.0;
        navigation.view.layer.shadowColor = UIColor.blackColor().CGColor

        self.window!.rootViewController = mainViewVC

        return true
    }
MainViewController.swift
//
//  MainViewController.swift
//  JianDao3
//
//  Created by licrifan on 15/10/24.
//  Copyright © 2015年 licrifan. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    var titleLabel:UILabel!

    let mainTabs = ["消息", "通讯录", "文档", "个人中心"]

    override func viewDidLoad() {
        super.viewDidLoad()

        NSLog("MainViewController viewDidLoad()")

        self.view.backgroundColor = UIColor.whiteColor()

        let tabbgView = UIView()
        tabbgView.frame = CGRectMake(0, 20, CGRectGetWidth(self.view.bounds), 44)
        //tabbgView.backgroundColor = UIColor(hexString: "#ff3333", alpha: 0.85)
        tabbgView.backgroundColor = UIColor.blackColor()
        self.view.addSubview(tabbgView)

        let statusView = UIView()
        statusView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 20)
        //statusView.backgroundColor = UIColor(hexString: "#ff3333")
        statusView.backgroundColor = UIColor.lightGrayColor()
        self.view.addSubview(statusView)

        let addMenu = UIImage(named:"add_white_24x24.png");
        let addButton = UIButton()
        addButton.frame = CGRectMake(self.view.bounds.width - 54, 0, 44.0, 44.0)
        addButton.addTarget(self, action:"showAddMenu:", forControlEvents:UIControlEvents.TouchUpInside)
        addButton.setImage(addMenu,forState:UIControlState.Normal)
        tabbgView.addSubview(addButton)

        let searchMenu = UIImage(named:"search_24x24.png");
        let searchButton = UIButton()
        searchButton.frame = CGRectMake(addButton.frame.origin.x - 44, 0, 44.0, 44.0)
        searchButton.addTarget(self, action:"jumpToSearch:", forControlEvents:UIControlEvents.TouchUpInside)
        searchButton.setImage(searchMenu,forState:UIControlState.Normal)
        tabbgView.addSubview(searchButton)

        titleLabel = UILabel(frame: CGRectMake(20, 20, self.view.bounds.width - 108, 44.0))
        titleLabel.textColor = UIColor.whiteColor()
        titleLabel.textAlignment = NSTextAlignment.Left
        titleLabel.font = UIFont.boldSystemFontOfSize(18)
        titleLabel.text = STR_APP_NAME
        self.view.addSubview(titleLabel)


        //create tabs
        let viewMessage         = MessageViewController()
        let viewContact         = ContactViewController()
        let viewFile            = FileViewController()
        let viewPersonalCenter  = PersonalCenterViewController()

        let nvcMessage = UINavigationController(rootViewController: viewMessage)
        nvcMessage.tabBarItem = UITabBarItem(title: mainTabs[0], image: UIImage(named:"message.png"), tag:0);
        let nvcContact = UINavigationController(rootViewController: viewContact)
        nvcContact.tabBarItem = UITabBarItem(title: mainTabs[1], image: UIImage(named:"contact.png"), tag:1);
        let nvcFile = UINavigationController(rootViewController: viewFile)
        nvcFile.tabBarItem = UITabBarItem(title: mainTabs[2], image: UIImage(named:"file.png"), tag:2);
        let nvcPersonalCenter = UINavigationController(rootViewController: viewPersonalCenter)
        nvcPersonalCenter.tabBarItem = UITabBarItem(title: mainTabs[3], image: UIImage(named:"peronal_center.png"), tag:3);

        self.viewControllers = [nvcMessage, nvcContact, nvcFile, nvcPersonalCenter]
        //default select message index 0
        self.selectedIndex = 0
    }

    //show add menu
    func showAddMenu(button : UIButton){

    }


    //jump to search view
    func jumpToSearch(button : UIButton){

    }
}
其他的,比如:
MessageViewController.swift
都是空的:
import UIKit

class MessageViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()


    }
}
效果是:
default blank screen iOS app
此处还是没有tab图片的。
需要去找一下。
经过一番折腾,代码如下:
AppDelegate.swift
相关代码:
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.

        //application.statusBarHidden = true;

        //init main view controller
        let mainViewVC = MainViewController()
        let navigation = UINavigationController(rootViewController:mainViewVC)

        navigation.view.layer.shadowOffset = CGSizeMake(0,0);
        navigation.view.layer.shadowOpacity = 0.5;
        navigation.view.layer.shadowRadius = 10.0;
        navigation.view.layer.shadowColor = UIColor.blackColor().CGColor

        self.window!.rootViewController = mainViewVC

        return true
    }
MainViewController.swift代码:
//
//  MainViewController.swift
//  JianDao3
//
//  Created by licrifan on 15/10/24.
//  Copyright © 2015年 licrifan. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    var titleLabel:UILabel!

    let mainTabs = ["消息", "通讯录", "文档", "个人中心"]

    override func viewDidLoad() {
        super.viewDidLoad()

        NSLog("MainViewController viewDidLoad()")

        self.view.backgroundColor = UIColor.whiteColor()

        let statusView = UIView()
        statusView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), STATUS_BAR_HEIGHT)
        //statusView.backgroundColor = UIColor(hexString: "#ff3333")
        statusView.backgroundColor = UIColor.lightGrayColor()
        self.view.addSubview(statusView)

        let tabbgView = UIView()
        tabbgView.frame = CGRectMake(0, 20, CGRectGetWidth(self.view.bounds), NAVI_BAR_HEIGHT)
        //tabbgView.backgroundColor = UIColor(hexString: "#ff3333", alpha: 0.85)
        tabbgView.backgroundColor = UIColor.blackColor()
        self.view.addSubview(tabbgView)

        let addMenu = UIImage(named:"add_white_24x24.png")
        let addButton = UIButton()
        addButton.frame = CGRectMake(self.view.bounds.width - 54, 0, 44.0, NAVI_BAR_HEIGHT)
        addButton.addTarget(self, action:"showAddMenu:", forControlEvents:UIControlEvents.TouchUpInside)
        addButton.setImage(addMenu,forState:UIControlState.Normal)
        tabbgView.addSubview(addButton)

        let searchMenu = UIImage(named:"search_24x24.png")
        let searchButton = UIButton()
        searchButton.frame = CGRectMake(addButton.frame.origin.x - 44, 0, 44.0, NAVI_BAR_HEIGHT)
        searchButton.addTarget(self, action:"jumpToSearch:", forControlEvents:UIControlEvents.TouchUpInside)
        searchButton.setImage(searchMenu,forState:UIControlState.Normal)
        tabbgView.addSubview(searchButton)

        titleLabel = UILabel(frame: CGRectMake(20, 20, self.view.bounds.width - 108, NAVI_BAR_HEIGHT))
        titleLabel.textColor = UIColor.whiteColor()
        titleLabel.textAlignment = NSTextAlignment.Left
        titleLabel.font = UIFont.boldSystemFontOfSize(20)
        titleLabel.text = STR_APP_NAME
        self.view.addSubview(titleLabel)


        //create tabs
        let viewMessage         = MessageViewController()
        let viewContact         = ContactViewController()
        let viewFile            = FileViewController()
        let viewPersonalCenter  = PersonalCenterViewController()

        let barItemFont = UIFont.systemFontOfSize(12)

        let nvcMessage = UINavigationController(rootViewController: viewMessage)
        nvcMessage.tabBarItem = UITabBarItem(title: mainTabs[0], image: UIImage(named:"message_unselected.png"), tag:0)
        //nvcMessage.tabBarItem.setTitleTextAttributes([NSFontAttributeName: barItemFont], forState: UIControlState.Normal)

        let nvcContact = UINavigationController(rootViewController: viewContact)
        nvcContact.tabBarItem = UITabBarItem(title: mainTabs[1], image: UIImage(named:"contact_unselected.png"), tag:1)
        //nvcContact.tabBarItem.setTitleTextAttributes([NSFontAttributeName: barItemFont], forState: UIControlState.Normal)

        let nvcFile = UINavigationController(rootViewController: viewFile)
        nvcFile.tabBarItem = UITabBarItem(title: mainTabs[2], image: UIImage(named:"file_unselected.png"), tag:2)
        //nvcFile.tabBarItem.setTitleTextAttributes([NSFontAttributeName: barItemFont], forState: UIControlState.Normal)

        let nvcPersonalCenter = UINavigationController(rootViewController: viewPersonalCenter)
        nvcPersonalCenter.tabBarItem = UITabBarItem(title: mainTabs[3], image: UIImage(named:"personal_center_unselected.png"), tag:3)
        //nvcPersonalCenter.tabBarItem.setTitleTextAttributes([NSFontAttributeName: barItemFont], forState: UIControlState.Normal)

        self.viewControllers = [nvcMessage, nvcContact, nvcFile, nvcPersonalCenter]

        //change bar item font to larger
        for nvcController in self.viewControllers!{
            nvcController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: barItemFont], forState: UIControlState.Normal)
        }

        //default select message index 0
        self.selectedIndex = 0
    }

    //show add menu
    func showAddMenu(button : UIButton){

    }


    //jump to search view
    func jumpToSearch(button : UIButton){

    }
}
最后效果为:
final jiandao app effect

转载请注明:在路上 » [已解决]Swift代码方式的去创建一个基于tab页面的app

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
86 queries in 0.175 seconds, using 22.10MB memory