Any given instance of UIView
can only appear in once in the view hierarchy. If you think about the APIs defined on UIView
, this is fairly obvious. For example, UIView
has a frame
property which defines it's location in the superview. The frame
property wouldn't make sense if the viewed appeared in two places.
So you need multiple instances. You can streamline your code by defining a method that creates separators:
- (UILabel *)newSeparator
{
UILabel *separatorLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 1, 44)];
separatorLabel.backgroundColor = [UIColor colorWithRGB:0xe5edec];
UIBarButtonItem *separator = [[UIBarButtonItem alloc] initWithCustomView:separatorLabel];
return separator;
}
And then you can add your items like this:
[items addObjectsFromArray:@[button1, [self newSeparator], button2, [self newSeparator]];
Also, you don't need to use UILabel
if you're only displaying a background color. You can just use UIView
.