With this simple project you can add and design your material round button like in Android apps.
- Add this package as dependcy
.package(url: "https://github.com/IhorShevchuk/TMFloatingButton", , from: "1.2.0")
- Import
TMFloatingButton
import TMFloatingButton
- Add Folder
TMFloatingButton
to project - Import file
TMFloatingButton.h
to files where buttons will be shown
- Xcode 11 or higher
- iOS 13.0 or higher
- ARC
- In some method (ex:
viewDidLoad
) initTMFloatingButton
object and add it to view where it should appear using next code:
TMFloatingButton *floatingModeEditButton = [[TMFloatingButton alloc] initWithSuperView: self.navigationController.view];
- Apply one of predifined styles using next code:
[TMFloatingButton addModeEditStyleToButton:floatingModeEditButton];
- Add
target
andselector
using next code:
[floatingModeEditButton addTarget:self action:@selector(modeEditAction) forControlEvents:UIControlEventTouchUpInside];
For now you can use next styles
- Favorite Style - Star button
- Mode Edit Style - Red button with pen
- Message Style - New message button
You can use one of next methods to create your own style
- To add state with text use next code:
[customTextButton addStateWithText:@"Custom text" withAttributes:textAttributesDictionary andBackgroundColor:bgColor forName:@"CustomTextState" applyRightNow:YES];
- To add state with icon use next code
[customIconButton addStateWithIcon:[UIImage imageNamed:@"icon-image"] andBackgroundColor:bgColor forName:@"CustomIconState" applyRightNow:NO];
- To add state with icon and text use next code
[customIconAndTextButton addStateWithIcon:[UIImage imageNamed:@"icon-image"] andText:@"Custom text" withAttributes:textAttributesDictionary andBackgroundColor:bgColor forName:@"CustomTextAndIconState" applyRightNow:NO];
- Or you can design your own UIView and set using next code
[customViewButton addStateWithView:customView andBackgroundColor:bgColor forName:@"CustomViewState" applyRightNow:YES];
BOOL
parameter ApplyRightNow is using when you need set this state immediatly after adding state- TextAttributesDictionary is
NSDictionary
of text attributes (ex:NSFontAttributeName
,NSForegroundColorAttributeName
etc.)
Sometimes button can change states during the app runing. Example add to favorites button that indicates if object(artilce,words,tags) added to favorites or not. And you can easy set this states with initing and after that just change states in different places in code
[button setButtonState:@"myState2"];
You can simply show UIActivityIndicatorView
using this code:
[floatingButton animateActivityIndicatorStart:YES];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[floatingButton setButtonState:@"saved"];
[floatingButton animateActivityIndicatorStart:NO];
});
See example code for more details
Ability to move button on long tap- Support SwiftUI
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.