Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Pop up badge, written in Swift 4. Add as subview to any UIView object

License

NotificationsYou must be signed in to change notification settings

DominikButz/DYBadge

Repository files navigation

VersionLicensePlatformCarthage compatible

DYBadge (written in Swift) is a UILabel subclass that can be added as subview to any UIView and its subclasses (except for UIImageView). If the string value of the badge changes from nil (or "nullish") to a valid string value at runtime the badge will appear with a pop up effect. If it switches back to a "nullish" value, it will shrink and disappear.The class also includes a UIButton class function (extension) to create a button with an image and a title, which is positioned underneath the image.since version 2.0, the xOffset and yOffset values have been removed, as well as badgeFont, badgeColor and badgeTextColor. Simply use the UILabel properties to set these values, e.g. textColor, backgroundColor etc. (don't use myBadge.text directly, use badgeString instead).

Example

To checkout the example project, simply clone the repo or download the zip file.

Features

  • Create in code or get a UILabel in Interface Builder and change its class to DYBadge
  • Create a UIButton with image and a button title placed underneath the image and add a DYBadge (see the extension in the DYBadge.swift file).

Installation

Installation through Cocoapods or Carthage is recommended.

Carthage: Simply add the following line to your Cartfile.

github "DominikButz/DYBadge" ~> 2.0

Check out the version history below for the current version.

Afterwards, run "carthage update DYBadge --platform iOS" in the root directory of your project. Follow the steps described in the carthage project on github (click on the carthage compatible shield above).

Alternatively, install through Cocoapods:

target '[project name]' dopod 'DYBadge', '~> 2.0end

Make sure to import DYBadge into your View Controller subclass:

import DYBadge

Usage

Check out the following examples.

Code example: Creating a button with a DYBadge

@IBOutlet weakvarfirstButton:UIButton!overridefunc viewDidLoad(){   super.viewDidLoad()letframe=CGRect(x:self.firstButton.frame.size.width-3.0, y:-5.0, width:8.0, height:5.0)letfirstBadge=DYBadge(frame: frame)letfont=UIFont(name:"HelveticaNeue-Bold", size:10.0)!      firstBadge.font= font   firstBadge.backgroundColor=UIColor.red   firstBadge.textColor=UIColor.white    // set a start value if required   firstBadge.badgeString="7"self.firstButton.addSubview(firstBadge)}

DYBadgeButton example

Code example: Creating a button with image and title

@IBOutlet weakvarsecondButtonItem:UIBarButtonItem!overridefunc viewDidLoad(){    super.viewDidLoad()letbuttonFrame=CGRect(x:0, y:0, width:25.0, height:26.0)letfilterButton=UIButton.createImageTitleButton(image:imageLiteral(resourceName:"filterButton"), title:"Filter", font: font, frame: buttonFrame, tintColor:self.view.tintColor)      // see extensionletsecondBadge=DYBadge(frame: secondBadgeFrame)       secondBadge.backgroundColor=self.view.tintColor       secondBadge.textColor=UIColor.white       secondBadge.font= font       filterButton.addSubview(secondBadge)self.secondButtonItem.customView= filterButton}

DYBadgeButton example

Cocoapods 1.5 error workaround.

An error message might be displayed (failed to load designables.... no suitable image found) and some storyboard elements might not be drawn correctly. This won't stop xcode from building your code. To remove this annoying error, add the code below to the end of your podfile:

// Workaround for Cocoapods v.1.5 issue #7606

post_install do |installer|installer.pods_project.build_configurations.each do |config|config.build_settings.delete('CODE_SIGNING_ALLOWED')config.build_settings.delete('CODE_SIGNING_REQUIRED')endend

Change log

minor corrections, settings update to swift 5.0

Changed spring damping value in animateBadgeTransform function. The former settingcould make a device or the simulator freeze (Xcode 10 / iOS 12) when the badge value was set to a null-type value.

re-added designable and inspectable key words. see the error workaround above.

removed designable and inspectable key words due to error messages in Interface builder.

source file path correction in podspec file.

xOffset and yOffset values have been removed, as well as badgeFont, badgeColor and badgeTextColor. Simply use the UILabel properties (font, color, backgroundColor) to set these values.

Minor build fix.

The badge is now placed by default at the origin of the superview. Adjust your offsetX and offsetY values accordingly - see the example above.

public header fix for carthage

adding podfile, readme update

missing source files in example - fix

see deprecated project DYBadgeButton for previous versions.

Author

dominikbutz@gmail.com

License

DYBadge is available under the MIT license. See the LICENSE file for more info.

About

Pop up badge, written in Swift 4. Add as subview to any UIView object

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp