Swift about Toolbar Learning notes

Source: Internet
Author: User
Tags deprecated

Import UIKit


Class Viewcontroller:uiviewcontroller {

@IBOutlet weak var toolbar:uitoolbar!


@IBOutlet weak var addactionlabel:uilabel!

Override Func Viewdidload () {

Super.viewdidload ()

Configuretoolbar ();

}

Configure the tool bar Item function

Func Configuretoolbar () {

Let Toolbarbuttonitem = [Addbuttonitem,

Flexiblespacebarbuttonitem,

Camerabuttonitem]

Toolbar.setitems (Toolbarbuttonitem, animated:true);

}


Tool bar System Add Button Item

var addbuttonitem:uibarbuttonitem{

Return Uibarbuttonitem (Barbuttonsystemitem:. ADD, target:self, Action: "Addaction:")

}

Tool bar System camera button Item

var camerabuttonitem:uibarbuttonitem{

Return Uibarbuttonitem (Barbuttonsystemitem:. Camera, target:self, Action: "Cameraaction:")

}

Item

Direct Springs between

Spring

var Flexiblespacebarbuttonitem:uibarbuttonitem {

Return Uibarbuttonitem (Barbuttonsystemitem:. Flexiblespace, Target:nil, Action:nil)

}


Add a button event

Func Addaction (Barbuttonitem:uibarbuttonitem) {

Addactionlabel.text = "Add Action something"

Print ("Add Action something")

}

Photo Button Event

Func cameraaction (Barbuttonitem:uibarbuttonitem) {

Addactionlabel.text = "Camera Something"


Print ("Camera something")

}

Override Func didreceivememorywarning () {

Super.didreceivememorywarning ()

Dispose of any resources the can be recreated.

}






Let's add a custom Buttonitem

Custom

var custombuttonitem:uibarbuttonitem{

Return Uibarbuttonitem (title: "ToolBar", Style:. Done, target:self, action: "CustomAction:")

}

How to add a spring and a Click event method

Item between Spring

var flexiblespacebarbuttontwo:uibarbuttonitem{

Return Uibarbuttonitem (Barbuttonsystemitem:. Flexiblespace, Target:nil, Action:nil);

}

Customizing the item event

Func CustomAction (Custombarbuttonitem:uibarbuttonitem) {

Addactionlabel.text = "Custom Something"

}

Most importantly modify the configuration function to put the newly added item into the array


Configure the tool bar Item function

Func Configuretoolbar () {

Let Toolbarbuttonitem = [Addbuttonitem,

Flexiblespacebarbuttonitem,

Camerabuttonitem,flexiblespacebarbuttontwo,custombuttonitem]

Toolbar.setitems (Toolbarbuttonitem, animated:true);

}





How do you think it's too ugly, okay, we can add our own background image to Buttonitem, which is often done in the project.

In OC We can change toolbar's style and Tincolor as well as we see the same API when we look at the Swift toolbar API

Look at the API

Enum Uibarstyle:int {

//

Case Default

Case Black

//

Deprecated. Use Uibarstyleblack

Case Blacktranslucent//Deprecated. Use Uibarstyleblack and set the translucent property to YES

//        }

I'll set the black transparent.

Toolbar.barstyle =. Blacktranslucent

Toolbar.translucent = true;

Toolbar.tintcolor = Uicolor.greencolor ()


Add an item to a picture

var customimagebuttonite:uibarbuttonitem{


Return Uibarbuttonitem (Image:uiimage (named: "Test"), style:. Done, target:self, action: "Customimagebuttonitem");


}


Swift about Toolbar Learning notes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.