HMSegmentedControl Tying up the segmented control to a scroll view in Swift? - ios

I am using the popular HMSegmentedControl to present segments in my project. But I am facing a disastrous problem now that I am unable to tie my segments to a scroll view. As the example is in objective C. Its becoming difficult to do so in Swift. The code goes like this -
self.segmentedControl4 = [[HMSegmentedControl alloc] initWithFrame:CGRectMake(0, 260, viewWidth, 50)];
self.segmentedControl4.sectionTitles = #[#"Worldwide", #"Local", #"Headlines"];
self.segmentedControl4.selectedSegmentIndex = 1;
self.segmentedControl4.backgroundColor = [UIColor colorWithRed:0.7 green:0.7 blue:0.7 alpha:1];
self.segmentedControl4.titleTextAttributes = #{NSForegroundColorAttributeName : [UIColor whiteColor]};
self.segmentedControl4.selectedTitleTextAttributes = #{NSForegroundColorAttributeName : [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:1]};
self.segmentedControl4.selectionIndicatorColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1];
self.segmentedControl4.selectionStyle = HMSegmentedControlSelectionStyleBox;
self.segmentedControl4.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationUp;
self.segmentedControl4.tag = 3;
__weak typeof(self) weakSelf = self;
[self.segmentedControl4 setIndexChangeBlock:^(NSInteger index)
{
[weakSelf.scrollView scrollRectToVisible:CGRectMake(viewWidth * index, 0, viewWidth, 200) animated:YES];
}];
[self.view addSubview:self.segmentedControl4];
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 310, viewWidth, 210)];
self.scrollView.backgroundColor = [UIColor colorWithRed:0.7 green:0.7 blue:0.7 alpha:1];
self.scrollView.pagingEnabled = YES;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.contentSize = CGSizeMake(viewWidth * 3, 200);
self.scrollView.delegate = self;
[self.scrollView scrollRectToVisible:CGRectMake(viewWidth, 0, viewWidth, 200) animated:NO];
[self.view addSubview:self.scrollView];
Can anyone who has worked on this third party give me some solutions?

Here is the code for scrolling a segmented control. You can also download the source from here.
import UIKit
import HSegmentControl
class ViewController: UIViewController, HSegmentControlDataSource {
#IBOutlet weak var segmentControl: HSegmentControl!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
segmentControl.dataSource = self
segmentControl.numberOfDisplayedSegments = 3
segmentControl.segmentIndicatorViewContentMode = UIViewContentMode.bottom
// segmentControl.selectedTitleFont = UIFont.boldSystemFont(ofSize: 17)
segmentControl.selectedTitleColor = UIColor.white
// segmentControl.unselectedTitleFont = UIFont.systemFont(ofSize: 17)
// segmentControl.unselectedTitleColor = UIColor.darkGray
// segmentControl.segmentIndicatorImage = UIImage(named: "ind_img")
segmentControl.segmentIndicatorView.backgroundColor = UIColor(red: 31.0/255.0, green: 48.0/255.0, blue: 93.0/255.0, alpha: 1.0)
segmentControl.selectedIndex = 2
}
// MARK: - HSegmentControlDataSource protocol
func numberOfSegments(_ segmentControl: HSegmentControl) -> Int {
return 5
}
func segmentControl(_ segmentControl: HSegmentControl, titleOfIndex index: Int) -> String {
return ["Kavin","Sitharth", "Raj", "Selva", "Rajesh"][index]
}
func segmentControl(_ segmentControl: HSegmentControl, segmentBackgroundViewOfIndex index: Int) -> UIView {
let view = UIView()
// view.backgroundColor = UIColor(red: CGFloat(drand48()), green: CGFloat(drand48()), blue: CGFloat(drand48()), alpha: 1)
return view
}
#IBAction func valueChanged(_ sender: AnyObject) {
print("value did change to \((sender as! HSegmentControl).selectedIndex)")
}
}

Just set up the segmented control normally and set the indexChangeBlock to scroll the scroll view:
segmentedControl4.indexChangeBlock = {
let frame = CGRectMake(viewWidth * index, 0, viewWidth, 200)
scrollView.scrollRectToVisible(frame, animated:true)
}
This will scroll the scroll view every time the segmented control's index changes.

Related

How to implement UI like iCarousel Rotary type, Objective-C?

I want to implement this type of UI (Horizontal Scrolling).
I know its kind of "iCarousel", Type = iCarouselTypeRotary. I tried with this library but I am getting this type UI. I cant able to customize fully:
If anyone know how to do this UI in native way orelse any library, please let me know. Any inputs will be appreciated.
The iCarousel library provides the iCarouselTypeCustom type.
The following is a custom example.
override func awakeFromNib() {
super.awakeFromNib()
for i in 0 ... 6 {
items.append(i)
}
}
override func viewDidLoad() {
super.viewDidLoad()
carousel.type = .custom
}
func numberOfItems(in carousel: iCarousel) -> Int {
return items.count
}
func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {
var label: UILabel
var itemView: UIImageView
if let view = view as? UIImageView {
itemView = view
label = itemView.viewWithTag(1) as! UILabel
} else {
itemView = UIImageView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
itemView.image = UIImage(named: "page.png")
itemView.layer.cornerRadius = 5
itemView.contentMode = .center
label = UILabel(frame: itemView.bounds)
label.backgroundColor = .clear
label.textAlignment = .center
label.font = label.font.withSize(50)
label.tag = 1
itemView.addSubview(label)
}
label.text = "\(items[index])"
return itemView
}
func carousel(_ carousel: iCarousel, valueFor option: iCarouselOption, withDefault value: CGFloat) -> CGFloat {
switch option {
case .visibleItems:
let elemento:Int = self.carousel.currentItemIndex as Int
print(elemento)
if elemento == 0 {
return 2;
}else if elemento == items.count-1 {
return 2;
}else {
return 3;
}
default:
return value
}
}
func carousel(_ carousel: iCarousel, itemTransformForOffset offset: CGFloat, baseTransform transform: CATransform3D) -> CATransform3D {
let offsetFactor:CGFloat = self.carousel(carousel, valueFor: .spacing, withDefault: 1.25) * carousel.itemWidth
let zFactor: CGFloat = 400.0
let normalFactor: CGFloat = 0
let shrinkFactor: CGFloat = 100.0
let f: CGFloat = sqrt(offset * offset + 1)-1
var trans = transform
trans = CATransform3DTranslate(trans, offset * offsetFactor, f * normalFactor, f * (-zFactor))
trans = CATransform3DScale(trans, 1 / (f / shrinkFactor + 1.0), 1 / (f / shrinkFactor + 1.0), 1.0 )
return trans
}
func carousel(_ carousel: iCarousel, shouldSelectItemAt index: Int) -> Bool {
return true
}
Finally I did with custom view and I have achieved UI as I need it. Here, I am providing my code for someone needy.
- (void)viewDidLoad
{
[super viewDidLoad];
_iCarouselItems = [[NSMutableArray alloc]init];
for (int i = 0; i < 10; i++)
{
[_iCarouselItems addObject:#(i)];
}
self.iCarouselView.dataSource = self;
self.iCarouselView.delegate = self;
_iCarouselView.type = iCarouselTypeCustom;
dispatch_async(dispatch_get_main_queue(), ^{
[_iCarouselView reloadData];
});
}
#pragma mark iCarousel methods
- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
return [_iCarouselItems count];
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
UILabel *label = nil;
//create new view if no view is available for recycling
if (view == nil)
{
NSLog(#"viewForItemAtIndex is %ld",(long)index);
//don’t do anything specific to the index within
//this `if (view == nil) {…}` statement because the view will be
//recycled and used with other index values later
view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200.0f)];
// ((UIImageView *)view).image = [UIImage imageNamed:#"smiley-400x400.jpg"];
view.contentMode = UIViewContentModeCenter;
view.backgroundColor = [UIColor whiteColor];
view.layer.cornerRadius = 8;
view.layer.masksToBounds = true;
view.layer.borderColor = [UIColor grayColor].CGColor;
view.layer.borderWidth = 2;
label = [[UILabel alloc] initWithFrame:view.bounds];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = NSTextAlignmentCenter;
label.font = [label.font fontWithSize:50];
label.tag = 1;
[view addSubview:label];
}
else
{
//get a reference to the label in the recycled view
label = (UILabel *)[view viewWithTag:1];
}
label.text = [_iCarouselItems[index] stringValue];
return view;
}
- (CATransform3D)carousel:(iCarousel *)carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform
{
CGFloat offsetFactor = [self carousel:carousel valueForOption:iCarouselOptionSpacing withDefault:0.3]*carousel.itemWidth;
CGFloat zFactor = 400.0;
CGFloat normalFactor = 0;
CGFloat shrinkFactor = 100.0;
CGFloat f = sqrt(offset * offset + 1)-1;
transform = CATransform3DTranslate(transform, offset * offsetFactor, f * normalFactor, f * (-zFactor));
transform = CATransform3DScale(transform, 1 / (f / shrinkFactor + 1.0), 1 / (f / shrinkFactor + 1.0), 1.0 );
return transform;
}
- (BOOL)carousel:(iCarousel *)carousel shouldSelectItemAtIndex:(NSInteger)index
{
return true;
}
- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{
NSLog(#"Selected carouselindex is %ld",(long)index);
}
- (NSInteger)numberOfPlaceholdersInCarousel:(iCarousel *)carousel
{
//note: placeholder views are only displayed on some carousels if wrapping is disabled
return 0;
}
There are several solutions available for 3D carousels.
Take a look at that one for example:https://www.cocoacontrols.com/controls/parallaxcarousel
You can find the source code on Github. This project uses CABasicAnimation und you can install it using CocaPods.

how to create property method in swift

I make one UITableView property in my class and make getter method for it like this:
#interface SideViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>
#property (nonatomic,strong) UITableView *table;
#end
#implementation SideViewController
#synthesize selectedIndex;
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
}
- (UITableView*)table {
if (!_table) {
CGRect tableFrame = CGRectMake(0,150,230, self.view.frame.size.height-150);
_table = [[UITableView alloc]initWithFrame:tableFrame style:UITableViewStylePlain];
_table.scrollEnabled = NO;
_table.delegate = self;
_table.dataSource = self;
_table.showsVerticalScrollIndicator = YES;
_table.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
[_table setBackgroundColor:[UIColor clearColor]];
_table.separatorStyle = UITableViewCellSeparatorStyleNone;
}
return _table;
}
when I call self.table get my table now I want write this code in swift but I'm so confused!!!
please guide me about that.
this is my swift code :
import UIKit
class SideViewController: UIViewController,UITableViewDelegate {
let table:UITableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.redColor()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
You can use lazy in swift:
class SideViewController: UIViewController, UITableViewDelegate {
lazy var table: UITableView = {
let tableFrame = CGRectMake(0, 150, 230, self.view.frame.size.height - 150)
let _table = UITableView(frame: tableFrame, style: UITableViewStyle.Plain)
_table.scrollEnabled = false
_table.delegate = self
_table.dataSource = self
_table.showsVerticalScrollIndicator = true
_table.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
_table.backgroundColor = UIColor.clearColor()
_table.separatorStyle = UITableViewCellSeparatorStyle.None
return _table
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.redColor()
}
}

If no Table View results, display "No Results" on screen

I have a tableview, where sometimes there might not be any results to list, so I would like to put something up that says "no results" if there are no results (either a label or one table view cell?).
Is there an easiest way to do this?
I would try a label behind the tableview then hide one of the two based on the results, but since I'm working with a TableViewController and not a normal ViewController I'm not sure how smart or doable that is.
I'm also using Parse and subclassing as a PFQueryTableViewController:
#interface TableViewController : PFQueryTableViewController
I can provide any additional details needed, just let me know!
TableViewController Scene in Storyboard:
EDIT: Per Midhun MP, here's the code I'm using
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSInteger numOfSections = 0;
if ([self.stringArray count] > 0)
{
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
numOfSections = 1;
//yourTableView.backgroundView = nil;
self.tableView.backgroundView = nil;
}
else
{
UILabel *noDataLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, self.tableView.bounds.size.height)];
noDataLabel.text = #"No data available";
noDataLabel.textColor = [UIColor blackColor];
noDataLabel.textAlignment = NSTextAlignmentCenter;
//yourTableView.backgroundView = noDataLabel;
//yourTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.backgroundView = noDataLabel;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
return numOfSections;
}
And here's the View I'm getting, it still has separator lines. I get the feeling that this is some small change, but I'm not sure why separator lines are showing up?
You can easily achieve that by using backgroundView property of UITableView.
Objective C:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSInteger numOfSections = 0;
if (youHaveData)
{
yourTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
numOfSections = 1;
yourTableView.backgroundView = nil;
}
else
{
UILabel *noDataLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, yourTableView.bounds.size.width, yourTableView.bounds.size.height)];
noDataLabel.text = #"No data available";
noDataLabel.textColor = [UIColor blackColor];
noDataLabel.textAlignment = NSTextAlignmentCenter;
yourTableView.backgroundView = noDataLabel;
yourTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
return numOfSections;
}
Swift:
func numberOfSections(in tableView: UITableView) -> Int
{
var numOfSections: Int = 0
if youHaveData
{
tableView.separatorStyle = .singleLine
numOfSections = 1
tableView.backgroundView = nil
}
else
{
let noDataLabel: UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: tableView.bounds.size.height))
noDataLabel.text = "No data available"
noDataLabel.textColor = UIColor.black
noDataLabel.textAlignment = .center
tableView.backgroundView = noDataLabel
tableView.separatorStyle = .none
}
return numOfSections
}
Reference UITableView Class Reference
backgroundView Property
The background view of the table view.
Declaration
Swift
var backgroundView: UIView?
Objective-C
#property(nonatomic, readwrite, retain) UIView *backgroundView
Discussion
A table view’s background view is automatically resized to match the
size of the table view. This view is placed as a subview of the table
view behind all cells, header views, and footer views.
You must set this property to nil to set the background color of the
table view.
For Xcode 8.3.2 - Swift 3.1
Here is a not-so-well-known but incredibly easy way to achieve adding a "No Items" view to an empty table view that goes back to Xcode 7. I'll leave it to you control that logic that adds/removes the view to the table's background view, but here is the flow for and Xcode (8.3.2) storyboard:
Select the scene in the Storyboard that has your table view.
Drag an empty UIView to the "Scene Dock" of that scene
Add a UILabel and any constraints to the new view and then create an IBOutlet for that view
Assign that view to the tableView.backgroundView
Behold the magic!
Ultimately this works anytime you want to add a simple view to your view controller that you don't necessarily want to be displayed immediately, but that you also don't want to hand code.
Swift Version of above code :-
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
var numOfSection: NSInteger = 0
if CCompanyLogoImage.count > 0 {
self.tableView.backgroundView = nil
numOfSection = 1
} else {
var noDataLabel: UILabel = UILabel(frame: CGRectMake(0, 0, self.tableView.bounds.size.width, self.tableView.bounds.size.height))
noDataLabel.text = "No Data Available"
noDataLabel.textColor = UIColor(red: 22.0/255.0, green: 106.0/255.0, blue: 176.0/255.0, alpha: 1.0)
noDataLabel.textAlignment = NSTextAlignment.Center
self.tableView.backgroundView = noDataLabel
}
return numOfSection
}
But If you are loading Information From a JSON , you need to check whether the JSON is empty or not , therefor if you put code like this it initially shows "No data" Message then disappear. Because after the table reload data the message hide. So You can put this code where load JSON data to an array. SO :-
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func extract_json(data:NSData) {
var error: NSError?
let jsonData: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.MutableContainers , error: &error)
if (error == nil) {
if let jobs_list = jsonData as? NSArray
{
if jobs_list.count == 0 {
var noDataLabel: UILabel = UILabel(frame: CGRectMake(0, 0, self.tableView.bounds.size.width, self.tableView.bounds.size.height))
noDataLabel.text = "No Jobs Available"
noDataLabel.textColor = UIColor(red: 22.0/255.0, green: 106.0/255.0, blue: 176.0/255.0, alpha: 1.0)
noDataLabel.textAlignment = NSTextAlignment.Center
self.tableView.backgroundView = noDataLabel
}
for (var i = 0; i < jobs_list.count ; i++ )
{
if let jobs_obj = jobs_list[i] as? NSDictionary
{
if let vacancy_title = jobs_obj["VacancyTitle"] as? String
{
CJobTitle.append(vacancy_title)
if let vacancy_job_type = jobs_obj["VacancyJobType"] as? String
{
CJobType.append(vacancy_job_type)
if let company_name = jobs_obj["EmployerCompanyName"] as? String
{
CCompany.append(company_name)
if let company_logo_url = jobs_obj["EmployerCompanyLogo"] as? String
{
//CCompanyLogo.append("http://google.com" + company_logo_url)
let url = NSURL(string: "http://google.com" + company_logo_url )
let data = NSData(contentsOfURL:url!)
if data != nil {
CCompanyLogoImage.append(UIImage(data: data!)!)
}
if let vacancy_id = jobs_obj["VacancyID"] as? String
{
CVacancyId.append(vacancy_id)
}
}
}
}
}
}
}
}
}
do_table_refresh();
}
func do_table_refresh() {
dispatch_async(dispatch_get_main_queue(), {
self.tableView.reloadData()
return
})
}
You can try this control. Its is pretty neat. DZNEmptyDataSet
Or if I were you all I would do is
Check to see if your data array is empty
If it is empty then add one object called #"No Data" to it
Display that string in cell.textLabel.text
Easy peasy
Swift 3 (updated):
override func numberOfSections(in tableView: UITableView) -> Int {
if myArray.count > 0 {
self.tableView.backgroundView = nil
self.tableView.separatorStyle = .singleLine
return 1
}
let rect = CGRect(x: 0,
y: 0,
width: self.tableView.bounds.size.width,
height: self.tableView.bounds.size.height)
let noDataLabel: UILabel = UILabel(frame: rect)
noDataLabel.text = "Custom message."
noDataLabel.textColor = UIColor.white
noDataLabel.textAlignment = NSTextAlignment.center
self.tableView.backgroundView = noDataLabel
self.tableView.separatorStyle = .none
return 0
}
Swift3.0
I hope it server your purpose......
In your UITableViewController .
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchController.isActive && searchController.searchBar.text != "" {
if filteredContacts.count > 0 {
self.tableView.backgroundView = .none;
return filteredContacts.count
} else {
Helper.EmptyMessage(message: ConstantMap.NO_CONTACT_FOUND, viewController: self)
return 0
}
} else {
if contacts.count > 0 {
self.tableView.backgroundView = .none;
return contacts.count
} else {
Helper.EmptyMessage(message: ConstantMap.NO_CONTACT_FOUND, viewController: self)
return 0
}
}
}
Helper Class with function :
/* Description: This function generate alert dialog for empty message by passing message and
associated viewcontroller for that function
- Parameters:
- message: message that require for empty alert message
- viewController: selected viewcontroller at that time
*/
static func EmptyMessage(message:String, viewController:UITableViewController) {
let messageLabel = UILabel(frame: CGRect(x: 0, y: 0, width: viewController.view.bounds.size.width, height: viewController.view.bounds.size.height))
messageLabel.text = message
let bubbleColor = UIColor(red: CGFloat(57)/255, green: CGFloat(81)/255, blue: CGFloat(104)/255, alpha :1)
messageLabel.textColor = bubbleColor
messageLabel.numberOfLines = 0;
messageLabel.textAlignment = .center;
messageLabel.font = UIFont(name: "TrebuchetMS", size: 18)
messageLabel.sizeToFit()
viewController.tableView.backgroundView = messageLabel;
viewController.tableView.separatorStyle = .none;
}
I think the most elegant way to solve your problem is switching from a UITableViewController to a UIViewController that contains a UITableView. This way you can add whatever UIView you want as subviews of the main view.
I wouldn't recommend using a UITableViewCell to do this you might need to add additional things in the future and things can quicky get ugly.
You can also do something like this, but this isn't the best solution either.
UIWindow* window = [[UIApplication sharedApplication] keyWindow];
[window addSubview: OverlayView];
Use this code in Your numberOfSectionsInTableView method:-
if ([array count]==0
{
UILabel *fromLabel = [[UILabel alloc]initWithFrame:CGRectMake(50, self.view.frame.size.height/2, 300, 60)];
fromLabel.text =#"No Result";
fromLabel.baselineAdjustment = UIBaselineAdjustmentAlignBaselines;
fromLabel.backgroundColor = [UIColor clearColor];
fromLabel.textColor = [UIColor lightGrayColor];
fromLabel.textAlignment = NSTextAlignmentLeft;
[fromLabel setFont:[UIFont fontWithName:Embrima size:30.0f]];
[self.view addSubview:fromLabel];
[self.tblView setHidden:YES];
}
I would present a an overlay view that has the look and message you want if the tableview has no results. You could do it in ViewDidAppear, so you have the results before showing/not showing the view.
SWIFT 3
let noDataLabel: UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: tableView.bounds.size.height))
noDataLabel.text = "No data available"
noDataLabel.textColor = UIColor.white
noDataLabel.font = UIFont(name: "Open Sans", size: 15)
noDataLabel.textAlignment = .center
tableView.backgroundView = noDataLabel
tableView.separatorStyle = .none
If you don't use the tableview footer and do not want the tableview to fill up the screen with empty default table cells i would suggest that you set your tableview footer to an empty UIView. I do not know the correct syntax for doing this in obj-c or Swift, but in Xamarin.iOS i would do it like this:
public class ViewController : UIViewController
{
UITableView _table;
public ViewController (IntPtr handle) : base (handle)
{
}
public override void ViewWillAppear(bool animated) {
// Initialize table
_table.TableFooterView = new UIView();
}
}
Above code will result in a tableview without the empty cells
Here is the solution that worked for me.
Add the following code to a new file.
Change your table class to the custom class "MyTableView" from storyboard or .xib
(this will work for the first section only. If you want to customize more, do changes in the MyTableView reloadData() function accordingly for other sections)
public class MyTableView: UITableView {
override public func reloadData() {
super.reloadData()
if self.numberOfRows(inSection: 0) == 0 {
if self.viewWithTag(1111) == nil {
let noDataLabel = UILabel()
noDataLabel.textAlignment = .center
noDataLabel.text = "No Data Available"
noDataLabel.tag = 1111
noDataLabel.center = self.center
self.backgroundView = noDataLabel
}
} else {
if self.viewWithTag(1111) != nil {
self.backgroundView = nil
}
}
}
}
If you want to do this without any code, try this!
Click on your tableView.
Change the style from "plain" to "grouped".
Now when you use ....
tableView.backgroundView = INSERT YOUR LABEL OR VIEW
It will not show the separators!
Add this code in one file and change your collection type to CustomCollectionView
import Foundation
class CustomCollectionView: UICollectionView {
var emptyModel = EmptyMessageModel()
var emptyView: EmptyMessageView?
var showEmptyView: Bool = true
override func reloadData() {
super.reloadData()
emptyView?.removeFromSuperview()
self.backgroundView = nil
if !showEmptyView {
return
}
if numberOfSections < 1 {
let rect = CGRect(x: 0,
y: 0,
width: self.bounds.size.width,
height: self.bounds.size.height)
emptyView = EmptyMessageView()
emptyView?.frame = rect
if let emptyView = emptyView {
// self.addSubview(emptyView)
self.backgroundView = emptyView
}
emptyView?.setView(with: emptyModel)
} else {
emptyView?.removeFromSuperview()
self.backgroundView = nil
}
}
}
class EmptyMessageView: UIView {
#IBOutlet weak var messageLabel: UILabel!
#IBOutlet weak var imageView: UIImageView!
var view: UIView!
override init(frame: CGRect) {
super.init(frame: frame)
xibSetup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
xibSetup()
}
func xibSetup() {
view = loadViewFromNib()
view.frame = bounds
view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
addSubview(view)
}
func loadViewFromNib() -> UIView {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: "EmptyMessageView", bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
return view
}
func setView(with model: EmptyMessageModel) {
messageLabel.text = model.message ?? ""
imageView.image = model.image ?? #imageLiteral(resourceName: "no_notification")
}
}
///////////
class EmptyMessageModel {
var message: String?
var image: UIImage?
init(message: String = "No data available", image: UIImage = #imageLiteral(resourceName: "no_notification")) {
self.message = message
self.image = image
}
}

change color navigation controller in a popover

I'm having a problem with trying to change the colour of my navigation controller inside my popoverController.
I'm trying to do something like this:
if (self.popoverController == nil) {
ArtistsViewController *artistsViewController =
[[ArtistsViewController alloc]
initWithNibName:#"ArtistsViewController"
bundle:[NSBundle mainBundle]];
artistsViewController.navigationItem.title = #"Artists";
UINavigationController *navController =
[[UINavigationController alloc]
initWithRootViewController:artistsViewController];
UIPopoverController *popover =
[[UIPopoverController alloc]
initWithContentViewController:artistsViewController.navigationController];
artistsViewController.navigationController.navigationBar.tintColor=[UIColor greenColor];
popover.delegate = self;
[artistsViewController release];
[navController release];
self.popoverController = popover;
[popover release];
}
[self.popoverController
presentPopoverFromBarButtonItem:sender
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
but it doesn't work. any suggestions?
////////////////////////////////////////
////////////**SOLUTION**////////
////////////////////////////////////////
I'm gonna edit this post because i solved my problem and I think could be helpful share my solution here:
first of all, follow this sample:
http://mobiforge.com/designing/story/using-popoverview-ipad-app-development
When It is done go to step two.
The second step will be add a new popoverBackgroundViewClass:
Add new file in your project Objective class and call it 'CustomPopoverBackgroundView'
///////////////////////////////////////////////
////// CustomPopoverBackgroundView.h /////////
///////////////////////////////////////////////
#import < UIKit/UIKit.h >
#import < UIKit/UIPopoverBackgroundView.h >
#interface CustomPopoverBackgroundView : UIPopoverBackgroundView{
UIPopoverArrowDirection direction;
CGFloat offset;
}
#end
//////////////////////////////////////////////
////// CustomPopoverBackgroundView.m /////////
//////////////////////////////////////////////
#import "CustomPopoverBackgroundView.h"
#implementation CustomPopoverBackgroundView
- (void)layoutSubviews {
[super layoutSubviews];
CGFloat fullHeight = self.frame.size.height;
CGFloat fullWidth = self.frame.size.width;
CGFloat startingLeft = 0.0;
CGFloat startingTop = 0.0;
CGFloat arrowCoord = 0.0;
UIImage *arrow;
UIImageView *arrowView;
switch (self.arrowDirection) {
case UIPopoverArrowDirectionUp:
startingTop += 13.0;
fullHeight -= 13.0;
//the image line.png will be the corner
arrow = [UIImage imageNamed:#"line.png"];
arrowCoord = (self.frame.size.width / 2) - self.arrowOffset;
arrowView = [[[UIImageView alloc] initWithFrame:CGRectMake(arrowCoord, 0, 13.0, 13.0)] autorelease];
break;
case UIPopoverArrowDirectionDown:
fullHeight -= 13.0;
arrow = [UIImage imageNamed:#"line.png"];
arrowCoord = (self.frame.size.width / 2) - self.arrowOffset;
arrowView = [[[UIImageView alloc] initWithFrame:CGRectMake(arrowCoord, fullHeight, 13.0, 13.0)] autorelease];
break;
case UIPopoverArrowDirectionLeft:
startingLeft += 13.0;
fullWidth -= 13.0;
arrow = [UIImage imageNamed:#"line.png"];
arrowCoord = (self.frame.size.height / 2) - self.arrowOffset;
arrowView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, arrowCoord, 13.0, 13.0)] autorelease];
break;
case UIPopoverArrowDirectionRight:
fullWidth -= 13.0;
arrow = [UIImage imageNamed:#"line.png"];
arrowCoord = (self.frame.size.height / 2) - self.arrowOffset;
arrowView = [[[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width-13.0, arrowCoord, 13.0, 13.0)] autorelease];
break;
}
//this image will be your background
UIImage *bg = [[UIImage imageNamed:#"lineBack.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(8.0, 8.0, 8.0, 8.0)];
UIImageView *imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(startingLeft, startingTop, fullWidth, fullHeight)] autorelease];
[imageView setImage:bg];
[arrowView setImage:arrow];
[self addSubview:imageView];
[self addSubview:arrowView];
}
- (CGFloat) arrowOffset {
return offset;
}
- (void) setArrowOffset:(CGFloat)arrowOffset {
offset = arrowOffset;
[self setNeedsLayout];
}
- (UIPopoverArrowDirection)arrowDirection {
return direction;
}
- (void)setArrowDirection:(UIPopoverArrowDirection)arrowDirection {
direction = arrowDirection;
[self setNeedsLayout];
}
+ (CGFloat)arrowHeight {
return 30.0;
}
+ (CGFloat)arrowBase {
return 30.0;
}
+ (UIEdgeInsets)contentViewInsets {
return UIEdgeInsetsMake(30.0, 30.0, 30.0, 30.0);
}
#end
Third step:
When It is done add this line in 'PopOverExample1ViewController.m':
import the new class:
#import " CustomPopoverBackgroundView.h "
-(IBAction) showMovies:(id) sender {
if (self.popoverController == nil) {
MoviesViewController *movies =
[[MoviesViewController alloc]
initWithNibName:#"MoviesViewController"
bundle:[NSBundle mainBundle]];
UIPopoverController *popover =
[[UIPopoverController alloc] initWithContentViewController:movies];
popover.delegate = self;
[movies release];
//THIS IS THE LINE THAT YOU HAVE TO ADD
popover.popoverBackgroundViewClass=[CustomPopoverBackgroundView class];
self.popoverController = popover;
[popover release];
}
[self.popoverController
presentPopoverFromBarButtonItem:sender
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
-(IBAction) btnShowMovies:(id) sender {
if (self.popoverController == nil) {
MoviesViewController *movies =
[[MoviesViewController alloc]
initWithNibName:#"MoviesViewController"
bundle:[NSBundle mainBundle]];
UIPopoverController *popover =
[[UIPopoverController alloc] initWithContentViewController:movies];
popover.delegate = self;
[movies release];
//THIS IS THE LINE THAT YOU HAVE TO ADD
popover.popoverBackgroundViewClass=[CustomPopoverBackgroundView class];
self.popoverController = popover;
[popover release];
}
CGRect popoverRect = [self.view convertRect:[btn frame]
fromView:[btn superview]];
popoverRect.size.width = MIN(popoverRect.size.width, 100);
[self.popoverController
presentPopoverFromRect:popoverRect
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
Alright!!! Thats all! If someOne needs help just let me know.
Best wishes!
override func viewDidAppear(_animated: Bool){
let navigationBar = self.navigationController?.navigationBar
navigationBar?.tintColor = UIColor(colorLiteralRed: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
the color of the button bar
navigationBar?.barStyle = UIBarStyle.black
the style bar
navigationBar?.barTintColor = UIColor(colorLiteralRed: 0.89, green: 0.55, blue: 0.69, alpha: 1.0)
the color of the bar
let imageView(frame: CGRect(x: 0, y: 0, width: 250, height: 80))
imageView.contentMode = .scaleAspectFit
}
image bar
all the code
override func viewDidAppear(_animated: Bool) {
let navigationBar = self.navigationController?.navigationBar
navigationBar?.tintColor = UIColor(colorLiteralRed: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
navigationBar?.barStyle = UIBarStyle.black
navigationBar?.barTintColor = UIColor(colorLiteralRed: 0.89, green: 0.55, blue: 0.69, alpha: 1.0)
let imageView(frame: CGRect(x: 0, y: 0, width: 250, height: 80))
imageView.contentMode = .scaleAspectFit
}

What is the android.widget.Toast equivalent for iOS applications?

I have made Android application a few months ago. The Toast class is very useful for me.
I do not need to consider the main Thread and place to show it. Anywhere I can show it and just leave that and it is automatically disappeared.
Toast.makeToast(context, msg, Toast.LENGTH_SHORT).show();
That's it. ^^
What about iPhone? Is there something like the Toast? Just show message and do not need to care about it. It will be automatically disappeared.
I've been writing for Android for a long time and I am missing Toast. I've implemented one. Need code? here you are:
ToastView.h
#import <UIKit/UIKit.h>
#interface ToastView : UIView
#property (strong, nonatomic) NSString *text;
+ (void)showToastInParentView: (UIView *)parentView withText:(NSString *)text withDuaration:(float)duration;
#end
ToastView.m
#import "ToastView.h"
#interface ToastView ()
#property (strong, nonatomic, readonly) UILabel *textLabel;
#end
#implementation ToastView
#synthesize textLabel = _textLabel;
float const ToastHeight = 50.0f;
float const ToastGap = 10.0f;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(UILabel *)textLabel
{
if (!_textLabel) {
_textLabel = [[UILabel alloc] initWithFrame:CGRectMake(5.0, 5.0, self.frame.size.width - 10.0, self.frame.size.height - 10.0)];
_textLabel.backgroundColor = [UIColor clearColor];
_textLabel.textAlignment = NSTextAlignmentCenter;
_textLabel.textColor = [UIColor whiteColor];
_textLabel.numberOfLines = 2;
_textLabel.font = [UIFont systemFontOfSize:13.0];
_textLabel.lineBreakMode = NSLineBreakByCharWrapping;
[self addSubview:_textLabel];
}
return _textLabel;
}
- (void)setText:(NSString *)text
{
_text = text;
self.textLabel.text = text;
}
+ (void)showToastInParentView: (UIView *)parentView withText:(NSString *)text withDuaration:(float)duration;
{
//Count toast views are already showing on parent. Made to show several toasts one above another
int toastsAlreadyInParent = 0;
for (UIView *subView in [parentView subviews]) {
if ([subView isKindOfClass:[ToastView class]])
{
toastsAlreadyInParent++;
}
}
CGRect parentFrame = parentView.frame;
float yOrigin = parentFrame.size.height - (70.0 + ToastHeight * toastsAlreadyInParent + ToastGap * toastsAlreadyInParent);
CGRect selfFrame = CGRectMake(parentFrame.origin.x + 20.0, yOrigin, parentFrame.size.width - 40.0, ToastHeight);
ToastView *toast = [[ToastView alloc] initWithFrame:selfFrame];
toast.backgroundColor = [UIColor darkGrayColor];
toast.alpha = 0.0f;
toast.layer.cornerRadius = 4.0;
toast.text = text;
[parentView addSubview:toast];
[UIView animateWithDuration:0.4 animations:^{
toast.alpha = 0.9f;
toast.textLabel.alpha = 0.9f;
}completion:^(BOOL finished) {
if(finished){
}
}];
[toast performSelector:#selector(hideSelf) withObject:nil afterDelay:duration];
}
- (void)hideSelf
{
[UIView animateWithDuration:0.4 animations:^{
self.alpha = 0.0;
self.textLabel.alpha = 0.0;
}completion:^(BOOL finished) {
if(finished){
[self removeFromSuperview];
}
}];
}
#end
Call from ViewController
[ToastView showToastInParentView:self.view withText:#"What a toast!" withDuaration:5.0];
There is no class "out-of-the-box" in UIKit to do this. But it is quite easy to create a class that will offer this behavior.
You just have to create a class that inherit from UIView. This class will have the responsibility
- to create what you want to display,
- to add itself in parent view hierarchy
- to dismiss itself using a timer.
You will be able to use it like :
[ToastView toastViewInView:myParentView withText:#"what a wonderful text"];
Regards,
Quentin
Have not tried but you might want to check:
https://github.com/ecstasy2/toast-notifications-ios
https://github.com/scalessec/Toast
You can do this in many ways one of the way is using UIAlertViewController() in swift3
let alertManager=UIAlertController(title: nil, message: "Welcome!", preferredStyle: .alert)
self.present(alertManager, animated: true, completion: nil)
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+1,
execute: {
alertManager.dismiss(animated: false, completion: nil)
})
Edit: Updated for Swift 3
Here is a Swift 3 version based on wojciech_maciejewski's answer. This looks more like Android Toast and doesn't stack toasts on each other. It draws toast into the center of the screen. It can handle long multiline texts.
import UIKit
class ToastView: UIView {
private static let hLabelGap: CGFloat = 40.0
private static let vLabelGap: CGFloat = 20.0
private static let hToastGap: CGFloat = 20.0
private static let vToastGap: CGFloat = 10.0
private var textLabel: UILabel!
static func showInParent(_ parentView: UIView, _ text: String, duration: Double = 3.0) {
let labelFrame = CGRect(x: parentView.frame.origin.x + hLabelGap,
y: parentView.frame.origin.y + vLabelGap,
width: parentView.frame.width - 2 * hLabelGap,
height: parentView.frame.height - 2 * vLabelGap)
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 15.0)
label.text = text
label.backgroundColor = UIColor.clear
label.textAlignment = NSTextAlignment.center
label.textColor = UIColor.white
label.numberOfLines = 0
label.frame = labelFrame
label.sizeToFit()
let toast = ToastView()
toast.textLabel = label
toast.addSubview(label)
toast.frame = CGRect(x: label.frame.origin.x - hToastGap,
y: label.frame.origin.y - vToastGap,
width: label.frame.width + 2 * hToastGap,
height: label.frame.height + 2 * vToastGap)
toast.backgroundColor = UIColor.darkGray
toast.alpha = 0.0
toast.layer.cornerRadius = 20.0
toast.center = parentView.center
label.center = CGPoint(x: toast.frame.size.width / 2, y: toast.frame.size.height / 2)
parentView.addSubview(toast)
UIView.animate(withDuration: 0.4, animations: {
toast.alpha = 0.9
label.alpha = 0.9
})
toast.perform(#selector(hideSelf), with: nil, afterDelay: duration)
}
#objc private func hideSelf() {
UIView.animate(withDuration: 0.4, animations: {
self.alpha = 0.0
self.textLabel.alpha = 0.0
}, completion: { t in self.removeFromSuperview() })
}
}
Usage from another controller:
ToastView.showInParent(navigationController!.view, "Hello world")
I'm posting swift version of Scarmysun answer:) many thanks
import Foundation
import UIKit
class ToastView: UIView {
static let toastHeight:CGFloat = 50.0
static let toastGap:CGFloat = 10;
lazy var textLabel: UILabel = UILabel(frame: CGRectMake(5.0, 5.0, self.frame.size.width - 10.0, self.frame.size.height - 10.0))
static func showInParent(parentView: UIView!, withText text: String, forDuration duration: double_t) {
//Count toast views are already showing on parent. Made to show several toasts one above another
var toastsAlreadyInParent = 0;
for view in parentView.subviews {
if (view.isKindOfClass(ToastView)) {
toastsAlreadyInParent++
}
}
var parentFrame = parentView.frame;
var yOrigin = parentFrame.size.height - getDouble(toastsAlreadyInParent)
var selfFrame = CGRectMake(parentFrame.origin.x + 20.0, yOrigin, parentFrame.size.width - 40.0, toastHeight);
var toast = ToastView(frame: selfFrame)
toast.textLabel.backgroundColor = UIColor.clearColor()
toast.textLabel.textAlignment = NSTextAlignment.Center
toast.textLabel.textColor = UIColor.whiteColor()
toast.textLabel.numberOfLines = 2
toast.textLabel.font = UIFont.systemFontOfSize(13.0)
toast.addSubview(toast.textLabel)
toast.backgroundColor = UIColor.darkGrayColor()
toast.alpha = 0.0;
toast.layer.cornerRadius = 4.0;
toast.textLabel.text = text;
parentView.addSubview(toast)
UIView.animateWithDuration(0.4, animations: {
toast.alpha = 0.9
toast.textLabel.alpha = 0.9
})
toast.performSelector(Selector("hideSelf"), withObject: nil, afterDelay: duration)
}
static private func getDouble(toastsAlreadyInParent : Int) -> CGFloat {
return (70.0 + toastHeight * CGFloat(toastsAlreadyInParent) + toastGap * CGFloat(toastsAlreadyInParent));
}
func hideSelf() {
UIView.animateWithDuration(0.4, animations: {
self.alpha = 0.0
self.textLabel.alpha = 0.0
}, completion: { t in self.removeFromSuperview() })
}
}

Resources